Vue 回调函数中使用this
使用回调函数操作页面数据或做路由跳转时不可直接使用this
,回调函数中的this
关键字并不是被执行方法中的this
,所以会出错,可以定义一个that
来保存被执行方法的this
,在回调函数中使用that
即可
const that = this
axios
.get("/api/index.php")
.then(function (res) {
//直接使用that访问
that.spinning = false
that.$router.push({name: 'Index'})
})