|
| 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