Subscribe on changes!

Sub RouterView component will render multiple times when using with KeepAlive

avatar
Sep 30th 2021

Version

3.2.19

Reproduction link

github.com

Steps to reproduce

  1. Visit Home Page.
  2. Jump to the About Page.
  3. Click button to render Sub Page.

What is expected?

Sub Page component render only once. The create hook called once.

What is actually happening?

Sub Page component render twice, and the created hook called twice.


I have did some research about this issue, hope it helps.

Since the Home Page is keep-alived,so when jump to the About Page, the Home Page won't be unmounted, and the render effect of the Home Page's sub RouterView won't be cleared.

And then when we click this button, the currentRoute of our router changed, which will trigger all the RouterView to be re rendered. Under normal conditions, the RouterView component inside About component will be render as the Sub component. But unfortunately, the RouterView component inside Home component also be re rendered as the Sub component.