解决Vue中的生命周期beforeDestory不触发的问题
分享一句很有用的经验:
给router-view加了个keep-alive导致组件缓存了,所以不会触发beforeDestory和destoryed
结束!
补充知识:vuexactions正确使用vue-resource的方式(Errorinmountedhook:"TypeError:Cannotreadproperty'get'ofu)
场景
.SPA中使用vuex初始化一项数据,在vuex的actions中需要使用vue-resource
使用的方式是
actions:{
setTaskList:function(store){
leturl='http://zhihu.carsonlius_liu.cn/api/tasks';
Vue.$http.get(url).then(function(response){
if(response.status===200){
store.commit('setTask',response.body);
}
});
}
}
报错提示
Errorinmountedhook:"TypeError:Cannotreadproperty'get'ofundefined
分析
.提示Vue.$http.get是不存在;打印之后果然不存在,所以问题就是Vue.上面了
.在actions里面打印console.log(Vue);
`warn('Vueisaconstructorandshouldbecalledwiththe`new`keyword');`
.所以尝试实例化Vue后的变量调用$http
解决
.声明Vue实列的常量并且依靠这个常量调用$http
constHttp=newVue
actions:{
setTaskList:function(store){
leturl='http://zhihu.carsonlius_liu.cn/api/tasks';
Http.$http.get(url).then(function(response){
if(response.status===200){
store.commit('setTask',response.body);
}
});
}
}
以上这篇解决Vue中的生命周期beforeDestory不触发的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短