在回调函数中:具有代表性的为axios的回调
普通函数
let _this=this
axios.get('/user', {
params: {
}
})
.then(function (response) {
console.log(_this.$route.name) //使用_this指向vue实例,可以获取到name
})
.catch(function (error) {
console.log(_this.$route.name) //使用_this指向vue实例,可以获取到name
});
箭头函数
axios.get('/user', {
params: {
}
})
.then(response => { //箭头函数中this指向vue实例
console.log(this.$route.name)
})
.catch(error => { //箭头函数中this指向vue实例
console.log(this.$route.name)
});