Skip to content

When using nested RouterView and KeepAlive, components aren't detached properly #2314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CrimsonFez opened this issue Jul 28, 2024 · 3 comments

Comments

@CrimsonFez
Copy link

Reproduction

https://github.com/CrimsonFez/vuejs-keep-alive-router-bug/

Steps to reproduce the bug

You need to have nested RouterView + KeepAlives like:

App.vue

<RouterView>
  <KeepAlive>
    <Component>

MyLayoutAndGetter1.vue

<RouterView>
  <KeepAlive>
    <Component :my-object="my-object">

MyLayoutAndGetter2.vue

<RouterView>
  <KeepAlive>
    <Component :my-object="my-object">

I believe that you also need to pass props through one of the router views, see https://github.com/CrimsonFez/vuejs-keep-alive-router-bug/blob/main/src/views/Person.vue

See my linked repo for full reproduction.

Expected behavior

This should behave like normal, when KeepAlive is not used

Actual behavior

When changing from one nested routerview to the other, it looks like props from the old router view are temporarily attached to the new router view

[Vue warn]: Missing required prop: "region" 
  at <RegionAbout onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< null > person= 
Object { id: "gandalf", name: "Gandalf", summary: "2000 year old pot smoking wizard" }
  ... > 
  at <KeepAlive key=0 > 
  at <RouterView key=0 > 
  at <Person personId="gandalf" onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< null >  ... > 
  at <KeepAlive key=0 > 
  at <RouterView> 
  at <App>

In my reproduction, this just causes an error, but on my larger app, this causes a crash.

Additional information

No response

@CrimsonFez
Copy link
Author

For anyone else with this issue, I found a work around. Give the 2nd level routerviews each a unique name, then in router.ts set that as the view for your child components.

@posva
Copy link
Member

posva commented Jul 29, 2024

Duplicate of #626

@posva posva marked this as a duplicate of #626 Jul 29, 2024
@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
@vuejs vuejs locked and limited conversation to collaborators Jul 29, 2024
@posva
Copy link
Member

posva commented Jul 29, 2024

Some of this behavior is inherent to KeepAlive and how it keeps updating components that are kept alive. Check this Vue playground to give you an idea of the behavior:

  • Change page twice
  • click increment
  • Both components keep updating in the background

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants