Skip to content

Commit ba05490

Browse files
committed
Update lazy-loading.md
1 parent 39f4cf0 commit ba05490

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

docs/en/advanced/lazy-loading.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22

33
When building apps with a bundler, the JavaScript bundle can become quite large, and thus affect the page load time. It would be more efficient if we can split each route's components into a separate chunk, and only load them when the route is visited.
44

5-
Combining Vue's [async component feature](http://vuejs.org/guide/components.html#Async-Components) and webpack's [code splitting feature](https://webpack.js.org/guides/code-splitting-require/), it's trivially easy to
5+
Combining Vue's [async component feature](http://vuejs.org/guide/components.html#Async-Components) and webpack's [code splitting feature](https://webpack.js.org/guides/code-splitting-async/), it's trivially easy to
66
lazy-load route components.
77

8-
All we need to do is define our route components as async components:
8+
First, an async component can be defined as a factory function that returns a Promise (which should resolve to the component itself):
99

1010
``` js
11-
const Foo = resolve => {
12-
// The `import()` call sets a code-split point.
13-
import('./Foo.vue').then(() => {
14-
resolve(require('./Foo.vue'))
15-
})
16-
}
11+
const Foo = () => Promise.resolve({ /* component definition */ })
1712
```
1813

19-
There's also an alternative code-split syntax using AMD style require, so this can be simplified to:
14+
Second, in webpack 2, we can use the [dynamic import](https://github.com/tc39/proposal-dynamic-import) syntax to indicate a code-split point:
2015

2116
``` js
22-
const Foo = resolve => require(['./Foo.vue'], resolve)
17+
import('./Foo.vue') // returns a Promise
18+
```
19+
20+
> Note: if you are using Babel, you will need to add the [syntax-dynamic-import](http://babeljs.io/docs/plugins/syntax-dynamic-import/) plugin so that Babel can properly parse the syntax.
21+
22+
Combining the two, this is how to define an async component that will be automatically code-split by webpack:
23+
24+
``` js
25+
const Foo = () => import('./Foo.vue')
2326
```
2427

2528
Nothing needs to change in the route config, just use `Foo` as usual:
@@ -34,12 +37,12 @@ const router = new VueRouter({
3437

3538
### Grouping Components in the Same Chunk
3639

37-
Sometimes we may want to group all the components nested under the same route into the same async chunk. To achieve that we need to use [named chunks](https://webpack.js.org/guides/code-splitting-require/#chunkname) by providing a chunk name to `require.ensure` as the 3rd argument:
40+
Sometimes we may want to group all the components nested under the same route into the same async chunk. To achieve that we need to use [named chunks](https://webpack.js.org/guides/code-splitting-async/#chunk-names) by providing a chunk name using a special comment syntax (requires webpack > 2.4):
3841

3942
``` js
40-
const Foo = r => require.ensure([], () => r(require('./Foo.vue')), 'group-foo')
41-
const Bar = r => require.ensure([], () => r(require('./Bar.vue')), 'group-foo')
42-
const Baz = r => require.ensure([], () => r(require('./Baz.vue')), 'group-foo')
43+
const Foo = () => import(/* webpackChunkName: "group-foo" */ './Foo.vue')
44+
const Bar = () => import(/* webpackChunkName: "group-foo" */ './Bar.vue')
45+
const Baz = () => import(/* webpackChunkName: "group-foo" */ './Baz.vue')
4346
```
4447

45-
webpack will group any async module with the same chunk name into the same async chunk - this also means we don't need to explicitly list dependencies for `require.ensure` anymore (thus passing an empty array).
48+
webpack will group any async module with the same chunk name into the same async chunk.

0 commit comments

Comments
 (0)