Vue-router: keep-alive inside a transition out-in reuses the old child route

Created on 12 Mar 2017  Â·  5Comments  Â·  Source: vuejs/vue-router

Vue.js / vue-router versions

Vue.js 2.2.2
vue-router 2.3.0

Reproduction Link

https://jsfiddle.net/2tdjec9q/

Steps to reproduce

step1: click to /b you will see this is B
step2: click to /c you will see this is C
step3: click to / you will see this is B not this is A

What is Expected?

at step3 i need this is A not this is B

What is actually happening?

at step3 i need this is A

if not use mode="out-in" this will run success

docs keep-alive

Most helpful comment

It looks like Vue is reusing the node but not replacing the content as it should. In your case you should key the router-view, otherwise, you won't get a transition between routes / and /b
https://jsfiddle.net/posva/jm2874L9/

IMO, when using a keep-alive combined with transitions, the router-view should always be keyed

All 5 comments

It looks like Vue is reusing the node but not replacing the content as it should. In your case you should key the router-view, otherwise, you won't get a transition between routes / and /b
https://jsfiddle.net/posva/jm2874L9/

IMO, when using a keep-alive combined with transitions, the router-view should always be keyed

@posva I spend few hours trying to fix this. Thanks a lot! Don't you think this should be documented?

this works indeed

@posva thank you ,,, but this way will build 2 layout

https://jsfiddle.net/u05ubyr5/

layout at / and /b have different data, build 2 layout item


I try to find origin of this issues, this is what i find:

when the keep-alive change the component

if have cache will just render this vnode

if (this.cache[key]) {
        vnode.componentInstance = this.cache[key].componentInstance;
      } else {

and if mode is out-in while not trigger view update, so the child router-view will not try to update

if there add a $forceUpdate like this:

if (this.cache[key]) {
        vnode.componentInstance = this.cache[key].componentInstance;
        vnode.componentInstance.$forceUpdate();
      } else {

it will work ~

but I know that this will lead to waste of resources, so can resolve by add

activated(){
this.$forceUpdate();
}

https://jsfiddle.net/g3snybbv/5/

@sqchenxiyuan You save my time!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cnweibo picture cnweibo  Â·  3Comments

alexstanbury picture alexstanbury  Â·  3Comments

posva picture posva  Â·  3Comments

Atinux picture Atinux  Â·  3Comments

saman picture saman  Â·  3Comments