【发布时间】:2017-04-18 13:09:29
【问题描述】:
这是我的代码:
export default {
data(){
return {
list: {}
}
},
components: {
listFields : ListFields
},
methods : {
submitForm(data){
let vm = this;
console.log(vm);
axios.post('/api/dashboard/lists/create', data)
.then(function (response) {
this.$router.push({path: 'show-list', params: {list_id: response.data.id}});
}).catch(function (error) {
console.log(error);
})
}
}
}
问题在于,在我调用“this.$router.push”的方法内部,它会引发错误,因为 this 引用了该函数。但问题是我无法引用 vm,因为我正在导出组件。我怎么解决这个问题?
【问题讨论】:
-
使用
vm.$router,顺便说一句,我不明白为什么你不能将它引用到vm以及它与导出的组件有什么关系?其他选项是在 axios 方法中使用箭头语法.then(response => { this.$router.push(//etc) }) -
我实际上更喜欢使用 .bind(this) 让匿名函数知道上下文。
-
您确定
$router已正确实例化吗?如果您将mounted处理程序添加到您的组件并且console.log(this.$router)它会返回一个值吗?如果没有,我们可以看看你是如何创建路由器的吗? -
@BertEvans 当我在 created() 方法中调用它时 - 它返回正确的值
标签: vue.js