Skip to content

Commit 1b6e575

Browse files
authored
docs (#137): add async component to migration guide (#139)
1 parent 9d67355 commit 1b6e575

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/.vuepress/config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ const sidebar = {
4848
children: [
4949
{
5050
title: 'Reactivity',
51-
children: ['/guide/reactivity', '/guide/reactivity-fundamentals', '/guide/reactivity-computed-watchers']
51+
children: [
52+
'/guide/reactivity',
53+
'/guide/reactivity-fundamentals',
54+
'/guide/reactivity-computed-watchers'
55+
]
5256
},
5357
{
5458
title: 'Composition API',
@@ -83,9 +87,11 @@ const sidebar = {
8387
title: 'Migration to Vue 3',
8488
collapsable: true,
8589
children: [
90+
'migration',
8691
'migration/global-api',
8792
'migration/treeshaking',
88-
'migration/functional-components'
93+
'migration/functional-components',
94+
'migration/async-components'
8995
]
9096
},
9197
{
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Async Components
2+
3+
## Overview
4+
5+
Here is a high level overview of what has changed:
6+
7+
- New `defineAsyncComponent` helper method that explicitly defines async components
8+
- `component` option renamed to `loader`
9+
- Loader function does not inherently receive `resolve` and `reject` arguments and must return a Promise
10+
11+
For a more in-depth explanation, read on!
12+
13+
## Introduction
14+
15+
Previously, async components were created by simply defining a component as a function that returned a promise, such as:
16+
17+
```js
18+
const asyncPage = () => import('./NextPage.vue')
19+
```
20+
21+
Or, for the more advanced component syntax with options:
22+
23+
```js
24+
const asyncPage = {
25+
component: () => import('./NextPage.vue'),
26+
delay: 200,
27+
timeout: 3000,
28+
error: ErrorComponent,
29+
loading: LoadingComponent
30+
}
31+
```
32+
33+
## Current Syntax
34+
35+
Now, in Vue 3, since functional components are defined as pure functions, async components definitions need to be explicitly defined by wrapping it in a new `defineAsyncComponent` helper:
36+
37+
```js
38+
import { defineAsyncComponent } from 'vue'
39+
import ErrorComponent from './components/ErrorComponent.vue'
40+
import LoadingComponent from './components/LoadingComponent.vue'
41+
42+
// Async component without options
43+
const asyncPage = defineAsyncComponent(() => import('./NextPage.vue'))
44+
45+
// Async component with options
46+
const asyncPageWithOptions = defineAsyncComponent({
47+
loader: () => import('./NextPage.vue'),
48+
delay: 200,
49+
timeout: 3000,
50+
errorComponent: ErrorComponent,
51+
loadingComponent: LoadingComponent
52+
})
53+
```
54+
55+
Another change that has been made from v2 is that the `component` option is now renamed to `loader` in order to accurately communicate that a component definition cannot be provided directly.
56+
57+
```js{4}
58+
import { defineAsyncComponent } from 'vue'
59+
60+
const asyncPageWithOptions = defineAsyncComponent({
61+
loader: () => import('./NextPAge.vue'),
62+
delay: 200,
63+
timeout: 3000,
64+
error: ErrorComponent,
65+
loading: LoadingComponent
66+
})
67+
```
68+
69+
In addition, unlike 2.x, the loader function no longer receives the `resolve` and `reject` arguments and must always return a Promise.
70+
71+
```js
72+
// 2.x version
73+
const oldAsyncComponent = (resolve, reject) => {
74+
/* ... */
75+
}
76+
77+
// 3.x version
78+
const asyncComponent = defineAsyncComponent(
79+
() =>
80+
new Promise((resolve, reject) => {
81+
/* ... */
82+
})
83+
)
84+
```
85+
86+
For more information on the usage of async components, see:
87+
88+
- [Guide: Dynamic & Async Components](/guide/component-dynamic-async.html#dynamic-components-with-keep-alive)

0 commit comments

Comments
 (0)