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. python相关问题求解决,有偿2. python - 请问用cxfreeze打包的可执行程序能反编译出源码么?3. python - 用__call__ 实现装饰器功能4. javascript - input输入框触发change事件后,外部元素会被选中5. javascript - 在sublime中如何给.vue文件注释?6. javascript - 把字符串日期转换成一般时间格式后,发现小于10的数字不会自动添加0在前面,如下7. javascript - vue2.0中router.repalce跟router.push有什么区别,二者的应用场景是会有怎样的区别?8. javascript - prismplayer播放器全屏问题9. javascript - js判断一个数组是否重复10. javascript - 这种上传图片预览怎么做?