场景
. SPA中 使用vuex初始化一项数据,在vuex的actions中需要使用vue-resource
使用的方式是
actions : {
setTaskList : function (store) {
let url = 'http://zhihu.carsonlius_liu.cn/api/tasks';
Vue.$http.get(url).then(function (response) {
if (response.status === 200) {
store.commit('setTask', response.body);
}
});
}
}
报错提示 Error in mounted hook: "TypeError: Cannot read property 'get' of undefined
分析
. 提示Vue.$http.get 是不存在;打印之后果然不存在, 所以问题就是Vue.上面了
. 在actions里面打印 console.log(Vue);
`warn('Vue is a constructor and should be called with the `new` keyword');`
. 所以尝试实例化Vue后的变量调用 $http
解决
. 声明Vue实列的常量 并且依靠这个常量调用$http
const Http = new Vue
actions : {
setTaskList : function (store) {
let url = 'http://zhihu.carsonlius_liu.cn/api/tasks';
Http.$http.get(url).then(function (response) {
if (response.status === 200) {
store.commit('setTask', response.body);
}
});
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)