vue报错之this.$router is undefine

最近写项目遇到这个报错,特此记录一下

1
2
3
4
5
6
axios.get('/person/ticket')
.then(response => {

this.$router.push('/ground')

})

原因很简单,因为我使用的是箭头函数,改变了this指向,所以vue在这里找不到$router,从而报错

解决很简单,保留this即可

1
2
3
4
5
6
7
const that = this;
axios.get('/person/ticket')
.then(response => {

that.$router.push('/ground')

})