node.js - 如何在 nuxtjs 中的组件方法而不是 fetch 方法中进行重定向?

标签 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)
        })
    }
  }

最佳答案

这对我有用

      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)
        })
    }
  }

关于node.js - 如何在 nuxtjs 中的组件方法而不是 fetch 方法中进行重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770213/

相关文章:

node.js - 在 nodejs 上循环 redis 返回的 promise

node.js - 具有 firebase : firebase. 功能的云功能不是功能

javascript - Node : Merge ES6 JS modules to one . min.js 文件

javascript - 如何在满足特定条件时删除 Vue 2 中的 eventListener (window.removeEventListener)

javascript - 非常简单的 vue.js 组件生成超出最大调用堆栈大小

javascript - Vue.js 将对象推送到数组会将数组中的每个元素更改为相同

linux - Nginx + Nodejs + WordPress 路由

html - 如何根据数据更改标签的CSS?

vue.js - Vue.Draggable 不允许我在列表之间移动项目

vue.js - vuejs 这在我的事件监听器中是未定义的