javascript - Under what circumstances is the component's life cycle beforeDestory triggered?
天蓬老师
天蓬老师 2017-07-05 10:46:40
0
2
1196

Can it be triggered when routing a jump? I found that the function in beforeDestory was not triggered when I made a route jump, but there was a piece of code that needed to be executed before the component was destroyed. What should I do?
https://jsfiddle.net/44w37p34/ A friend just sent me a demo. I found that his beforeDestory was triggered when the route jumped, but mine never worked. What is the reason?

天蓬老师
天蓬老师

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

reply all(2)
漂亮男人

I found the reason. I added a keep-alive to the router-view before, causing the component to be cached, so beforeDestory and destoryed will not be triggered

習慣沉默

I tried it, and it can trigger the execution of the beforeDestroy method when switching routes. The example is as follows.

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");
        }
    };
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template