@@ -5,27 +5,52 @@ Vue.use(VueRouter)
5
5
6
6
const Home = { template : '<div>home</div>' }
7
7
8
- // define Foo & Bar as async components.
9
- // async components are defined as: resolve => { resolve(Component) }
10
-
11
- // In Webpack we can use the AMD require syntax to signify a "split point"
8
+ // In Webpack we can use special require syntax to signify a "split point"
12
9
// Webpack will automatically split and lazy-load the split modules.
13
10
// - https://webpack.github.io/docs/code-splitting.html
14
11
12
+ // Combine that with Vue's async components, we can easily make our route
13
+ // components lazy-loaded only when the given route is matched.
14
+
15
+ // async components are defined as:
16
+ // - resolve => resolve(Component)
17
+ // or
18
+ // - () => Promise<Component>
19
+
20
+ // For single component, we can use the AMD shorthand
21
+ // require(['dep'], dep => { ... })
15
22
const Foo = resolve => require ( [ './Foo.vue' ] , resolve )
16
- const Bar = resolve => require ( [ './Bar.vue' ] , resolve )
17
23
18
24
// If using Webpack 2, you can also do:
19
25
// const Foo = () => System.import('./Foo.vue')
20
26
27
+ // If you want to group a number of components that belong to the same
28
+ // nested route in the same async chunk, the syntax is a bit more verbose.
29
+ // The repetition of the ensured list is necessary because the dependencies
30
+ // must be statically analyzable by Webpack. (Please let me know if you
31
+ // have a better way of handling this!)
32
+ const Bar = resolve => require . ensure ( [ './Bar.vue' , './Baz.vue' ] , ( ) => {
33
+ resolve ( require ( './Bar.vue' ) )
34
+ } )
35
+
36
+ const Baz = resolve => require . ensure ( [ './Bar.vue' , './Baz.vue' ] , ( ) => {
37
+ resolve ( require ( './Baz.vue' ) )
38
+ } )
39
+
21
40
const router = new VueRouter ( {
22
41
mode : 'history' ,
23
42
base : __dirname ,
24
43
routes : [
25
44
{ path : '/' , component : Home } ,
26
45
// Just use them normally in the route config
27
46
{ path : '/foo' , component : Foo } ,
28
- { path : '/bar' , component : Bar }
47
+ // Bar and Baz belong to the same root route
48
+ // and grouped in the same async chunk.
49
+ { path : '/bar' , component : Bar ,
50
+ children : [
51
+ { path : 'baz' , component : Baz }
52
+ ]
53
+ }
29
54
]
30
55
} )
31
56
@@ -38,6 +63,7 @@ new Vue({
38
63
<li><router-link to="/">/</router-link></li>
39
64
<li><router-link to="/foo">/foo</router-link></li>
40
65
<li><router-link to="/bar">/bar</router-link></li>
66
+ <li><router-link to="/bar/baz">/bar/baz</router-link></li>
41
67
</ul>
42
68
<router-view class="view"></router-view>
43
69
</div>
0 commit comments