Skip to content

Commit 132999a

Browse files
committed
update lazy-loading example
1 parent 11d0305 commit 132999a

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

examples/lazy-loading/Bar.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div>
33
<h2>{{ msg }}</h2>
44
<p>I am lazy-loaded. (check out the Networks tab in Chrome devtools)</p>
5+
<router-view></router-view>
56
</div>
67
</template>
78

examples/lazy-loading/Baz.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<h3>Baz</h3>
4+
<p>I'm loaded in the same chunk with Bar.</p>
5+
</div>
6+
</template>

examples/lazy-loading/app.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,52 @@ Vue.use(VueRouter)
55

66
const Home = { template: '<div>home</div>' }
77

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"
129
// Webpack will automatically split and lazy-load the split modules.
1310
// - https://webpack.github.io/docs/code-splitting.html
1411

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 => { ... })
1522
const Foo = resolve => require(['./Foo.vue'], resolve)
16-
const Bar = resolve => require(['./Bar.vue'], resolve)
1723

1824
// If using Webpack 2, you can also do:
1925
// const Foo = () => System.import('./Foo.vue')
2026

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+
2140
const router = new VueRouter({
2241
mode: 'history',
2342
base: __dirname,
2443
routes: [
2544
{ path: '/', component: Home },
2645
// Just use them normally in the route config
2746
{ 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+
}
2954
]
3055
})
3156

@@ -38,6 +63,7 @@ new Vue({
3863
<li><router-link to="/">/</router-link></li>
3964
<li><router-link to="/foo">/foo</router-link></li>
4065
<li><router-link to="/bar">/bar</router-link></li>
66+
<li><router-link to="/bar/baz">/bar/baz</router-link></li>
4167
</ul>
4268
<router-view class="view"></router-view>
4369
</div>

0 commit comments

Comments
 (0)