You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/advanced/lazy-loading.md
+18-15Lines changed: 18 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -2,24 +2,27 @@
2
2
3
3
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.
4
4
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
6
6
lazy-load route components.
7
7
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):
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:
20
15
21
16
```js
22
-
constFoo=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
+
constFoo= () =>import('./Foo.vue')
23
26
```
24
27
25
28
Nothing needs to change in the route config, just use `Foo` as usual:
@@ -34,12 +37,12 @@ const router = new VueRouter({
34
37
35
38
### Grouping Components in the Same Chunk
36
39
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):
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