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 - vue-cli 装好了 新建项目的好了,找不到项目是怎么回事?2. 网页动画等过渡效果,CSS3 transitions 和 jQuery animations 谁实现的性能更好?3. 用CSS3 box-sizing 属性实现两个并排的容器,如果想让容器中间有间隔该如何实现4. python3.x - python 中的maketrans在utf-8文件中该怎么使用5. javascript - 一排三个框,各个框的间距是15px,距离外面的白框间距也是15px,这个css怎么写?6. node.js - webpack 配置文件 运行出错7. mysql - C#连接数据库时一直这一句出问题int i = cmd.ExecuteNonQuery();8. javascript - hexo可以配置文章文件名为随机数么?9. css - ul ol前边的标记如何调整样式呢10. python - 用urllib抓取网页上的下载链接,目标文件是xls形式,但发现抓下来的xls是空表,里面只有一句报错信息,求帮助。

网公网安备