File tree 5 files changed +113
-1
lines changed
5 files changed +113
-1
lines changed Original file line number Diff line number Diff line change
1
+ import Vue from 'vue'
2
+ import VueRouter from 'vue-router'
3
+
4
+ // 1. Use plugin.
5
+ // This installs <router-view> and <router-link>,
6
+ // and injects $router and $route to all router-enabled child components
7
+ Vue . use ( VueRouter )
8
+
9
+ // 2. Define route components
10
+ const Home = { template : '<div>Component: home</div>' }
11
+ const Foo = { template : '<div>Component: foo</div>' }
12
+ const Bar = { template : '<div>Component: bar</div>' }
13
+
14
+ // 3. Create the router
15
+ const router = new VueRouter ( {
16
+ mode : 'history' ,
17
+ base : __dirname ,
18
+ routes : [
19
+ { path : '/' , component : Home } ,
20
+ { path : '/foo' , component : Foo } ,
21
+ { path : '/bar' , component : Bar }
22
+ ]
23
+ } )
24
+
25
+ // 4. Create extended base Vue with router injected here (all
26
+ // children should inherit the same router).
27
+ const BaseVue = Vue . extend ( { router } )
28
+
29
+ // Discrete components means that a new Vue instance will be created
30
+ // and bound on multiple *independent* nodes (eg. one Vue instance
31
+ // per node); but the router should act as a singleton and keep all
32
+ // instances in sync.
33
+ document . querySelectorAll ( '.app' ) . forEach ( ( node ) => {
34
+ new BaseVue ( {
35
+ el : node
36
+ } )
37
+ } )
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < link rel ="stylesheet " href ="/global.css ">
3
+ < style >
4
+ .inliner {font-size : 0 ;line-height : 0 ;}
5
+ .app {font-size : 1rem ;line-height : 1 ;display : inline-block;padding : 1rem ;width : 33% ;border-left : 1px solid # f1f1f1 ;box-sizing : border-box;vertical-align : top;}
6
+ .snippet {display : inline-block;padding : 5px ;background : # f1f1f1 ;font-size : 90% ;}
7
+ .app .component-view {display : block;width : 100% ;text-align : center;}
8
+ </ style >
9
+ < a href ="/ "> ← Examples index</ a >
10
+ < div class ="inliner ">
11
+ < div class ="app ">
12
+ < ul >
13
+ < li > < router-link to ="/ "> /</ router-link > </ li >
14
+ < li > < router-link to ="/foo "> /foo</ router-link > </ li >
15
+ < li > < router-link to ="/bar "> /bar</ router-link > </ li >
16
+ </ ul >
17
+ $route.path value: < span class ="snippet "> {{ $route.path }}</ span >
18
+ </ div >
19
+ < div class ="app ">
20
+ < ul >
21
+ < li > < router-link to ="/ "> /</ router-link > </ li >
22
+ < li > < router-link to ="/foo "> /foo</ router-link > </ li >
23
+ < li > < router-link to ="/bar "> /bar</ router-link > </ li >
24
+ </ ul >
25
+ $route.path value: < span class ="snippet "> {{ $route.path }}</ span >
26
+ </ div >
27
+ < div class ="app ">
28
+ < ul >
29
+ < li > < router-link to ="/ "> /</ router-link > </ li >
30
+ < li > < router-link to ="/foo "> /foo</ router-link > </ li >
31
+ < li > < router-link to ="/bar "> /bar</ router-link > </ li >
32
+ </ ul >
33
+ $route.path value: < span class ="snippet "> {{ $route.path }}</ span >
34
+ </ div >
35
+ </ div >
36
+ < div class ="app component-view ">
37
+ < router-view class ="view "> </ router-view >
38
+ $route.path value: < span class ="snippet "> {{ $route.path }}</ span >
39
+ </ div >
40
+ < script src ="/__build__/shared.js "> </ script >
41
+ < script src ="/__build__/discrete-components.js "> </ script >
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ <h1>Vue Router Examples</h1>
23
23
< li > < a href ="scroll-behavior "> Scroll Behavior</ a > </ li >
24
24
< li > < a href ="lazy-loading "> Lazy Loading</ a > </ li >
25
25
< li > < a href ="auth-flow "> Auth Flow</ a > </ li >
26
+ < li > < a href ="discrete-components "> Discrete Components</ a > </ li >
26
27
</ ul >
27
28
</ body >
28
29
</ html >
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ export default class VueRouter {
16
16
static version : string ;
17
17
18
18
app : any ;
19
+ apps : Array < any > ;
19
20
options : RouterOptions ;
20
21
mode : string ;
21
22
history : HashHistory | HTML5History | AbstractHistory ;
@@ -26,6 +27,7 @@ export default class VueRouter {
26
27
27
28
constructor ( options : RouterOptions = { } ) {
28
29
this . app = null
30
+ this . apps = [ ]
29
31
this . options = options
30
32
this . beforeHooks = [ ]
31
33
this . afterHooks = [ ]
@@ -69,6 +71,13 @@ export default class VueRouter {
69
71
`before creating root instance.`
70
72
)
71
73
74
+ this . apps . push ( app )
75
+
76
+ // main app already initialized.
77
+ if ( this . app ) {
78
+ return
79
+ }
80
+
72
81
this . app = app
73
82
74
83
const history = this . history
@@ -89,7 +98,9 @@ export default class VueRouter {
89
98
}
90
99
91
100
history . listen ( route => {
92
- this . app . _route = route
101
+ this . apps . forEach ( ( app ) => {
102
+ app . _route = route
103
+ } )
93
104
} )
94
105
}
95
106
Original file line number Diff line number Diff line change
1
+ import Vue from 'vue'
2
+ import VueRouter from '../../../src/index'
3
+
4
+ describe ( '[Vue Instance].$route bindings' , ( ) => {
5
+ describe ( 'boundToSingleVueInstance' , ( ) => {
6
+ it ( 'updates $route on all instances' , ( ) => {
7
+ const router = new VueRouter ( {
8
+ routes : [
9
+ { path : '/' , component : { name : 'foo' } } ,
10
+ { path : '/bar' , component : { name : 'bar' } }
11
+ ]
12
+ } )
13
+ const app1 = new Vue ( { router } )
14
+ const app2 = new Vue ( { router } )
15
+ expect ( app1 . $route . path ) . toBe ( '/' )
16
+ expect ( app2 . $route . path ) . toBe ( '/' )
17
+ router . push ( '/bar' )
18
+ expect ( app1 . $route . path ) . toBe ( '/bar' )
19
+ expect ( app2 . $route . path ) . toBe ( '/bar' )
20
+ } )
21
+ } )
22
+ } )
You can’t perform that action at this time.
0 commit comments