如何在组件方法中的nuxtjs中进行重定向而不是获取方法?

时间:2017-06-26 23:39:42

标签: node.js vue.js vuejs2 nuxt.js

我使用nuxtjs,我想在登录后重定向用户,redirect()方法在我的函数内部无效:

 loginUser: function () {
    if (this.isValid) {
      return axios.post(`/user/login`, {email: this.login.email, password: this.login.password})
        .then((res) => {
          let response = res.data
          if (typeof response.token !== 'undefined') {
            setToken(response.token)
            window.location.href = '/'
          }
        }).catch((e) => {
          console.log(e.message)
        })
    }
  }

同时访问url属性不起作用

 loginUser: function () {
    if (this.isValid) {
      return axios.post(`/user/login`, {email: this.login.email, password: this.login.password})
        .then((res) => {
          let response = res.data
          if (typeof response.token !== 'undefined') {
            console.log('loggedin')
            this.props.url.replace('/')
          }
        }).catch((e) => {
          console.log(e.message)
        })
    }
  }

1 个答案:

答案 0 :(得分:1)

这对我有用

      loginUser: function () {
    if (this.isValid) {
      return axios.post(`/user/login`, {email: this.login.email, password: this.login.password})
        .then((res) => {
          let response = res.data
          if (typeof response.token !== 'undefined') {
            setToken(response.token)
            console.log('loggedin')
            this.$router.replace('/')
          }
        }).catch((e) => {
          console.log(e.message)
        })
    }
  }