解決Vue中的生命周期beforeDestory不觸發(fā)的問(wèn)題
分享一句很有用的經(jīng)驗(yàn):
給router-view加了個(gè)keep-alive導(dǎo)致組件緩存了,所以不會(huì)觸發(fā)beforeDestory和destoryed
結(jié)束!
補(bǔ)充知識(shí):vuex actions正確使用vue-resource的方式( Error in mounted hook: 'TypeError: Cannot read property ’get’ of u)
場(chǎng)景
. SPA中 使用vuex初始化一項(xiàng)數(shù)據(jù),在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); } }); }}
報(bào)錯(cuò)提示
Error in mounted hook: 'TypeError: Cannot read property ’get’ of undefined
分析
. 提示Vue.$http.get 是不存在;打印之后果然不存在, 所以問(wèn)題就是Vue.上面了
. 在actions里面打印 console.log(Vue);
`warn(’Vue is a constructor and should be called with the `new` keyword’);`
. 所以嘗試實(shí)例化Vue后的變量調(diào)用 $http
解決
. 聲明Vue實(shí)列的常量 并且依靠這個(gè)常量調(diào)用$http
const Http = new Vueactions : { 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); } }); }}
以上這篇解決Vue中的生命周期beforeDestory不觸發(fā)的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介4. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問(wèn)題分析5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. SharePoint Server 2019新特性介紹7. React+umi+typeScript創(chuàng)建項(xiàng)目的過(guò)程8. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過(guò)程解析10. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
