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

问题描述 投票:0回答:2

你好伙计们我​​是vue新手,我使用的是vue 2.5,我做了很多google并发现了这个。$ router.push('/ login');但是我没有工作,所以很少有人混淆我是如何实现这一点的。

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

这是我的代码

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

注意我想在注册后将用户重定向到登录URL

vue.js axios
2个回答
2
投票

看起来你使用this.$router.push('/login');

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


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');
    }
}) 
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.