vue-router中Cannot read property 'push' of undefined的问题

方法1:在外部定义一个值指代Vue实例

var self = this;
this.$http.get("login", {
    params: data
}).then(function(response) {
    self.$router.push('index');
}).catch(function(response) {
    console.error(response);
});

方法2:使用ES6的箭头函数实现上下文穿透

this.$http.get("login", {
    params: data
}).then((response) => {
    this.$router.push('index'); // 此处因为ES6箭头函数上下文穿透,this的上下文为外层的this,即Vue实例
}).catch(function (response) {
    console.error(response);
});

猜你喜欢

转载自blog.csdn.net/weixin_37380784/article/details/88846400