javascript - 請問一下組件的生命周期beforeDestory是在什么情況下面觸發的呢?
問題描述
進行路由跳轉的時候能觸發嗎?我發現我進行路由跳轉的時候沒有觸發beforeDestory里面的函數,但是有段代碼需要在組件銷毀之前執行,請問我應該怎么做呢?https://jsfiddle.net/44w37p34/ 剛剛有個朋友給我發了一個demo,我發現他的beforeDestory在路由跳轉的時候觸發了,但是我的一直都不行,請問是什么原因呢
問題解答
回答1:找到原因了,之前給router-view加了個keep-alive導致組件緩存了,所以不會觸發beforeDestory和destoryed
回答2:我試了一下,切換路由的時候能觸發執行beforeDestroy方法,示例如下。
const Home = {template: ` <p> <p class='section'>Some section foo</p> <p class='section'>Some section foo</p> <p class='section'>Some section foo</p> <p class='section'>Some section foo</p> </p> `,mounted() { console.log('Home mounted');},beforeDestroy() { console.log('Home destroy');} }; const Test = {template: ` <p> <p class='section'>Some section test</p> <p class='section'>Some section test</p> <p class='section'>Some section test</p> <p class='section'>Some section test</p> </p> `,mounted() { console.log('Test mounted');},beforeDestroy() { console.log('Test destroy');} };
相關文章:
