在使用axios进行请求里,我们一般用this.$router.push('')的方法进行路由跳转:
this.$axios.post("/auth", {
'username': this.username,
'password': this.password
}).then(function (res) {
this.$router.push('/');
}).catch(function (error) {
console.log(error);
});
看上去没有任何问题,但是运行会报错:
‘$router’of undefined ???
这里需要对axios请求做一些修改:
this.$axios.post("/auth", {
'username': this.username,
'password': this.password
}).then(function (res) {
this.$router.push('/');
}.bind(this)).catch(function (error) {
console.log(error);
});
在axios的请求后面加上.bind(this)就可以运行成功了