解決Vue中的生命周期beforeDestory不觸發的問題
分享一句很有用的經驗:
給router-view加了個keep-alive導致組件緩存了,所以不會觸發beforeDestory和destoryed
結束!
補充知識:vuex actions正確使用vue-resource的方式( Error in mounted hook: 'TypeError: Cannot read property ’get’ of u)
場景
. 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 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不觸發的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. IntelliJ IDEA設置條件斷點的方法步驟2. Express 框架中使用 EJS 模板引擎并結合 silly-datetime 庫進行日期格式化的實現方法3. IIS Express 取代 ASP.NET Development Server的配置方法4. IntelliJ Idea2017如何修改緩存文件的路徑5. HTML <!DOCTYPE> 標簽6. Jsp中request的3個基礎實踐7. javascript設計模式 ? 建造者模式原理與應用實例分析8. Spring-Richclient 0.1.0 發布9. 一篇文章帶你了解JavaScript-對象10. python flask框架快速入門
