ktsn/vue-router-layout

The lifecycle function that enters the page for the first time is executed twice.

wwRandom opened this issue · 5 comments

When jumping from one page (without a layout file) to another page (with a layout file), the lifecycle function that enters the page for the first time is executed twice.

ktsn commented

Can you provide a reproduction to confirm and fix it?

ktsn commented

This is because of what vue-router-layout works. If the destination page component is an async component, it needs to wait loading to read layout option in the component options. So the steps will be like the below:

(When moving from page A (with layout X) to page B (with layout Y))

  1. Unmount page A, keep layout X since we don't know the page B's layout yet.
  2. Mount page B and wait loading it (Vue does it for us)
  3. After loading page B, we know B uses layout Y.
  4. Unmount layout X and mount layout Y. On the same time, page B will be unmounted and re-mount a new instance of it because Vue cannot detect the old page B and new one is the exact same VNode.

@wenGuangYang Does this behavior provides any real problem on your project, by the way?

@ktsn Yes! When we request data in the created method, it will be more than once. At the same time, when we write the method in the layout file, the first execution will report an error. Nuxt layout does not have this problem!

ktsn commented

This has been fixed on v0.1.5. Thank you for reporting this!