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. javascript - 在vue-cli引入vux后 使用报错2. angular.js - protractor初学 参考案例运行测试文件 报Error: Timeout3. android - 如何缩小APK的体积4. angular.js - angular中的controller 的js文件如何修改css样式比如margin:0?5. android - 京东移动端网页和其app加载的url所做的呈现不应该是完全一样的吗?6. position:absolute、float、display:inline-block 都能实现相同效果,区别是什么?7. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””8. CSS3 flex 如何让高度不等的同排等高?9. vue.js - vue 打包后 nginx 服务端API请求跨域问题无法解决。10. android - viewpager内部壳套fragment横向滑动。黑屏之后打开出现重叠现象,只有一个fragment,我复用的。望大神解答

网公网安备