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');} };
相关文章:
1. html5 - 百度Ueditor代码高亮和代码段滚动条冲突是怎么回事?2. javascript - 求解答,koa-bodyparser获取到的参数是空对象,为什么?????3. angular.js - `ng-bind-html`中的`ng-click`指令不生效4. html - nginx配置文件多个域名SSL如何配置?5. javascript - 关于Js中 this的一道题6. angular.js - 两个direcitve如何获取值7. HTML 5输入框只能输入汉字、字母、数字、标点符号?正则如何写?8. android - Genymotion 模拟器可以做屏幕适配检测吗?9. linux - 升级到Python3.6后GDB无法正常运行?10. angular.js - angularjs中如何实现单击一个span标签之后,拿到span标签中的内容?
![HTML 5输入框只能输入汉字、字母、数字、标点符号?正则如何写?](http://www.haobala.com/attached/image/10.jpg)