@@ -9,14 +9,6 @@ export function install (Vue) {
9
9
10
10
_Vue = Vue
11
11
12
- Object . defineProperty ( Vue . prototype , '$router' , {
13
- get ( ) { return this . $root . _router }
14
- } )
15
-
16
- Object . defineProperty ( Vue . prototype , '$route' , {
17
- get ( ) { return this . $root . _route }
18
- } )
19
-
20
12
const isDef = v => v !== undefined
21
13
22
14
const registerInstance = ( vm , callVal ) => {
@@ -29,9 +21,12 @@ export function install (Vue) {
29
21
Vue . mixin ( {
30
22
beforeCreate ( ) {
31
23
if ( isDef ( this . $options . router ) ) {
24
+ this . _routerRoot = this
32
25
this . _router = this . $options . router
33
26
this . _router . init ( this )
34
27
Vue . util . defineReactive ( this , '_route' , this . _router . history . current )
28
+ } else {
29
+ this . _routerRoot = ( this . $parent && this . $parent . _routerRoot ) || this
35
30
}
36
31
registerInstance ( this , this )
37
32
} ,
@@ -40,6 +35,14 @@ export function install (Vue) {
40
35
}
41
36
} )
42
37
38
+ Object . defineProperty ( Vue . prototype , '$router' , {
39
+ get ( ) { return this . _routerRoot . _router }
40
+ } )
41
+
42
+ Object . defineProperty ( Vue . prototype , '$route' , {
43
+ get ( ) { return this . _routerRoot . _route }
44
+ } )
45
+
43
46
Vue . component ( 'router-view' , View )
44
47
Vue . component ( 'router-link' , Link )
45
48
0 commit comments