進(jìn)行路由跳轉(zhuǎn)的時(shí)候能觸發(fā)嗎?我發(fā)現(xiàn)我進(jìn)行路由跳轉(zhuǎn)的時(shí)候沒有觸發(fā)beforeDestory裡面的函數(shù),但是有段程式碼需要在元件銷毀之前執(zhí)行,請(qǐng)問我該怎麼做呢?
https://jsfiddle.net/44w37p34/ 剛剛有個(gè)朋友給我發(fā)了一個(gè)demo,我發(fā)現(xiàn)他的beforeDestory在路由跳轉(zhuǎn)的時(shí)候觸發(fā)了,但是我的一直都不行,請(qǐng)問是什麼原因呢
歡迎選擇我的課程,讓我們一起見證您的進(jìn)步~~
找到原因了,之前給router-view加了個(gè)keep-alive導(dǎo)致元件快取了,所以不會(huì)觸發(fā)beforeDestory和destoryed
我試了一下,切換路由的時(shí)候能觸發(fā)執(zhí)行beforeDestroy方法,範(fàn)例如下。
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");
}
};