如何在ajax成功的基础上转移到另一条路线

时间:2018-03-05 13:50:46

标签: vue.js axios

你好我刚接触vue,我使用的是vue 2.5,我做了很多谷歌并发现了这个。$ router.push(' / login');但是我没有工作,所以我很难搞清楚如何实现这一点。

我想在用户成功注册后重定向到登录网址,然后我想将用户重定向到登录网址

这是我的代码

  let res = {
              phone: this.registerData.phone,
              companyname: this.registerData.companyname,
              username: this.registerData.username,
              email: this.registerData.email,
              password: this.registerData.password,
             }

         // console.log(res);
            var _this = this
            var vm = this.hasErrors
            var _vm = this.errorMessage
            this.$http.post('http://localhost:3000/api/users', res)
            .then(function (response) {

           if(response.status=== 200){
            console.log('move');

            this.$router.push('/login');
           }else{
            console.log('unknow');
           }
            }) 

注意我想在注册

后将用户重定向到登录网址

2 个答案:

答案 0 :(得分:2)

您似乎使用this.$router.push('/login');

而不是_this.$router.push('/login');

答案 1 :(得分:1)

您还可以使用箭头功能并在回调中使用this关键字,例如

let res = {
    phone:this.registerData.phone,
    companyname: this.registerData.companyname,
    username: this.registerData.username,
    email: this.registerData.email,
    password: this.registerData.password,
}

// console.log(res);
var vm = this.hasErrors
var _vm = this.errorMessage
this.$http.post('http://localhost:3000/api/users', res)
.then((response) => {
    if(response.status=== 200){
        console.log('move');

        this.$router.push('/login');
    }else{
        console.log('unknow');
    }
}) 
相关问题