javascript - 请问一下组件的生命周期beforeDestory是在什么情况下面触发的呢?
天蓬老师
天蓬老师 2017-07-05 10:46:40
0
2
1153

进行路由跳转的时候能触发吗?我发现我进行路由跳转的时候没有触发beforeDestory里面的函数,但是有段代码需要在组件销毁之前执行,请问我应该怎么做呢?
https://jsfiddle.net/44w37p34/ 刚刚有个朋友给我发了一个demo,我发现他的beforeDestory在路由跳转的时候触发了,但是我的一直都不行,请问是什么原因呢

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
漂亮男人

找到原因了,之前给router-view加了个keep-alive导致组件缓存了,所以不会触发beforeDestory和destoryed

習慣沉默

我试了一下,切换路由的时候能触发执行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");
        }
    };
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!