@@ -10,11 +10,14 @@ export default {
10
10
render ( h , { props, children, parent, data } ) {
11
11
data . routerView = true
12
12
13
+ const name = props . name
13
14
const route = parent . $route
14
15
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.
15
19
let depth = 0
16
20
let inactive = false
17
-
18
21
while ( parent ) {
19
22
if ( parent . $vnode && parent . $vnode . data . routerView ) {
20
23
depth ++
@@ -24,30 +27,33 @@ export default {
24
27
}
25
28
parent = parent . $parent
26
29
}
27
-
28
30
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
+
29
37
const matched = route . matched [ depth ]
38
+ // render empty node if no matched route
30
39
if ( ! matched ) {
40
+ cache [ name ] = null
31
41
return h ( )
32
42
}
33
43
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 ]
38
45
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
51
57
}
52
58
}
53
59
0 commit comments