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/guide/advanced/lazy-loading.md
+20-22Lines changed: 20 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,26 @@
1
1
# Lazy Loading Routes
2
2
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.
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 chunks, and only load them when the route is visited.
4
4
5
5
<!-- TODO: fix links -->
6
6
7
-
TODO: make it clear that this also works with other bundlers, not only with webpack
7
+
<!--TODO: make it clear that this also works with other bundlers, not only with webpack-->
8
8
9
-
Combining Vue's [async component feature](https://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 lazy-load route components.
9
+
Vue Router supports [dynamic imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports) out of the box, meaning you can replace static imports with dynamic ones:
10
10
11
-
First, an async component can be defined as a factory function that returns a Promise (which should resolve to the component itself):
The `component` (and `components`) option accepts a function that returns a Promise of a component and Vue Router **will only fetch it when entering the page for the first time**, then use the cached version. Which means you can also have more complex functions as long as they return a Promise:
12
24
13
25
```js
14
26
constUserDetails= () =>
@@ -17,29 +29,15 @@ const UserDetails = () =>
17
29
})
18
30
```
19
31
20
-
Second, in webpack 2, we can use the [dynamic import](https://github.com/tc39/proposal-dynamic-import) syntax to indicate a code-split point:
21
-
22
-
```js
23
-
import('./UserDetails.vue') // returns a Promise
24
-
```
32
+
In general, it's a good idea **to always use dynamic imports** for all your routes.
25
33
26
34
::: tip Note
27
-
if you are using Babel, you will need to add the [syntax-dynamic-import](https://babeljs.io/docs/plugins/syntax-dynamic-import/) plugin so that Babel can properly parse the syntax.
35
+
Do **not** use [Async components](https://v3.vuejs.org/guide/component-dynamic-async.html#async-components) for routes. Async components can still be used inside route components but route component themselves are just dynamic imports.
28
36
:::
29
37
30
-
Combining the two, this is how to define an async component that will be automatically code-split by webpack:
38
+
When using a bundler like webpack, this will automatically benefit from [code splitting](https://webpack.js.org/guides/code-splitting/)
When using Babel, you will need to add the [syntax-dynamic-import](https://babeljs.io/docs/plugins/syntax-dynamic-import/) plugin so that Babel can properly parse the syntax.
0 commit comments