代码如下:
login(){
// let that = this;
//地址http://localhost:8000/user/login、参数this.user
axios
.post("http://localhost:8000/user/login",this.user)
.then(function(response){
if(response.data=="登录成功"){
//返回主界面
this.$router.push("/home");
}
})
}
修改后:
login(){
let that = this;
//地址http://localhost:8000/user/login、参数this.user
axios
.post("http://localhost:8000/user/login",this.user)
.then(function(response){
if(response.data=="登录成功"){
//返回主界面
that.$router.push("/home");
}
})
}
问题描述:
this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象