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. python3.x - python多线程如何修改数据?2. css - width设置为100%之后列表无法居中3. python - 字符串里的u’u00a1’怎么打印成中文4. docker内创建jenkins访问另一个容器下的服务器问题5. mysql连表排序6. sublime-text - sublime text 3中编译python(sublimeREPL),如何仅运行单行/选定部分7. 代码如下,java用BigDecimal四舍五入怎么还有问题呀?哪种方式是完全没有问题的?8. 登录界面URL在服务器上无法找到9. javascript - 如何让双核浏览器默认选择急速模式渲染页面10. docker-compose 为何找不到配置文件?

网公网安备