How to solve "Uncaught TypeError: Cannot read property 'get' of undefined"? (Vue.JS 2)
How to solve "Uncaught TypeError: Cannot read property 'get' of undefined"? (Vue.JS 2)
我的组件是这样的:
methods: {
reloadMessage() {
setTimeout(function () {
this.$http.get(window.BaseUrl + '/message/inbox');
}, 1500);
}
}
我的路线是这样的:
Route::group(['prefix' => 'message','as'=>'messeage.'],function(){
Route::get('inbox', ['as'=>'inbox','uses'=>'MessageController@index']);
});
代码执行时,出现如下错误:
Uncaught TypeError: Cannot read property 'get' of undefined
我该如何解决?
你失去了上下文,
使用绑定:
methods: {
reloadMessage() {
setTimeout(function () {
this.$http.get(window.BaseUrl + '/message/inbox');
}.bind(this), 1500);
}
}
或箭头函数:
methods: {
reloadMessage() {
setTimeout(() => this.$http.get(window.BaseUrl + '/message/inbox'), 1500);
}
}
我的组件是这样的:
methods: {
reloadMessage() {
setTimeout(function () {
this.$http.get(window.BaseUrl + '/message/inbox');
}, 1500);
}
}
我的路线是这样的:
Route::group(['prefix' => 'message','as'=>'messeage.'],function(){
Route::get('inbox', ['as'=>'inbox','uses'=>'MessageController@index']);
});
代码执行时,出现如下错误:
Uncaught TypeError: Cannot read property 'get' of undefined
我该如何解决?
你失去了上下文, 使用绑定:
methods: {
reloadMessage() {
setTimeout(function () {
this.$http.get(window.BaseUrl + '/message/inbox');
}.bind(this), 1500);
}
}
或箭头函数:
methods: {
reloadMessage() {
setTimeout(() => this.$http.get(window.BaseUrl + '/message/inbox'), 1500);
}
}