猿问

请问一下组件的生命周期beforeDestory是在什么情况下面触发的呢?

进行路由跳转的时候能触发吗?我发现我进行路由跳转的时候没有触发beforeDestory里面的函数,但是有段代码需要在组件销毁之前执行,请问我应该怎么做呢?
https://jsfiddle.net/44w37p34/ 刚刚有个朋友给我发了一个demo,我发现他的beforeDestory在路由跳转的时候触发了,但是我的一直都不行,请问是什么原因呢

富国沪深
浏览 4074回答 1
1回答

子衿沉夜

我试了一下,切换路由的时候能触发执行beforeDestroy方法,示例如下。const Home = {&nbsp; &nbsp; &nbsp; &nbsp; template: `&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; <div class="section">Some section foo</div>&nbsp; &nbsp; &nbsp; <div class="section">Some section foo</div>&nbsp; &nbsp; &nbsp; <div class="section">Some section foo</div>&nbsp; &nbsp; &nbsp; <div class="section">Some section foo</div>&nbsp; &nbsp; </div>&nbsp; `,&nbsp; &nbsp; &nbsp; &nbsp; mounted() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log("Home mounted");&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; beforeDestroy() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Home destroy");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; const Test = {&nbsp; &nbsp; &nbsp; &nbsp; template: `&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="section">Some section test</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="section">Some section test</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="section">Some section test</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="section">Some section test</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; `,&nbsp; &nbsp; &nbsp; &nbsp; mounted() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Test mounted");&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; beforeDestroy() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Test destroy");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答