Skip to content

Commit ca02c9b

Browse files
author
hamilton1226
committed
<router-view>: keep previous view when tree is toggled inactive but kept-alive. (partially address vuejs/vue#4590)
1 parent 41e3ac5 commit ca02c9b

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/components/view.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ export default {
1010
render (h, { props, children, parent, data }) {
1111
data.routerView = true
1212

13+
const name = props.name
1314
const route = parent.$route
1415
const cache = parent._routerViewCache || (parent._routerViewCache = {})
16+
17+
// determine current view depth, also check to see if the tree
18+
// has been toggled inactive but kept-alive.
1519
let depth = 0
1620
let inactive = false
17-
1821
while (parent) {
1922
if (parent.$vnode && parent.$vnode.data.routerView) {
2023
depth++
@@ -24,30 +27,33 @@ export default {
2427
}
2528
parent = parent.$parent
2629
}
27-
2830
data.routerViewDepth = depth
31+
32+
// render previous view if the tree is inactive and kept-alive
33+
if (inactive) {
34+
return h(cache[name], data, children)
35+
}
36+
2937
const matched = route.matched[depth]
38+
// render empty node if no matched route
3039
if (!matched) {
40+
cache[name] = null
3141
return h()
3242
}
3343

34-
const name = props.name
35-
const component = inactive
36-
? cache[name]
37-
: (cache[name] = matched.components[name])
44+
const component = cache[name] = matched.components[name]
3845

39-
if (!inactive) {
40-
const hooks = data.hook || (data.hook = {})
41-
hooks.init = vnode => {
42-
matched.instances[name] = vnode.child
43-
}
44-
hooks.prepatch = (oldVnode, vnode) => {
45-
matched.instances[name] = vnode.child
46-
}
47-
hooks.destroy = vnode => {
48-
if (matched.instances[name] === vnode.child) {
49-
matched.instances[name] = undefined
50-
}
46+
// inject instance registration hooks
47+
const hooks = data.hook || (data.hook = {})
48+
hooks.init = vnode => {
49+
matched.instances[name] = vnode.child
50+
}
51+
hooks.prepatch = (oldVnode, vnode) => {
52+
matched.instances[name] = vnode.child
53+
}
54+
hooks.destroy = vnode => {
55+
if (matched.instances[name] === vnode.child) {
56+
matched.instances[name] = undefined
5157
}
5258
}
5359

0 commit comments

Comments
 (0)