Skip to content

Commit cb7415c

Browse files
committed
wip: async components
1 parent aca6388 commit cb7415c

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/guide/built-ins/suspense.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Suspense
2+
3+
## Basic Usage
4+
5+
### `async setup()`
6+
7+
### Async Components
8+
9+
Async components are _suspensible_ by default. This means if it has a `<Suspense>` in the parent chain, it will be treated as an async dependency of that `<Suspense>`. In this case, the loading state will be controlled by the `<Suspense>`, and the component's own loading, error, delay and timeout options will be ignored.
10+
11+
The async component can opt-out of `Suspense` control and let the component always control its own loading state by specifying `suspensible: false` in its options.
12+
13+
## Loading State
14+
15+
## Error Handling

src/guide/components/async.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,56 @@
11
# Async Components
22

3-
In large applications, we may need to divide the app into smaller chunks and only load a component from the server when it's needed. To make that possible, Vue has a `defineAsyncComponent` method:
3+
In large applications, we may need to divide the app into smaller chunks and only load a component from the server when it's needed. To make that possible, Vue has a [`defineAsyncComponent`](/api/general.html#defineasynccomponent) method:
44

55
```js
6-
const { createApp, defineAsyncComponent } = Vue
6+
const { defineAsyncComponent } = Vue
77

8-
const app = createApp({})
9-
10-
const AsyncComp = defineAsyncComponent(
11-
() =>
12-
new Promise((resolve, reject) => {
13-
resolve({
14-
template: '<div>I am async!</div>'
15-
})
16-
})
17-
)
18-
19-
app.component('async-example', AsyncComp)
8+
const AsyncComp = defineAsyncComponent(() => {
9+
return new Promise((resolve, reject) => {
10+
// ...load component from server
11+
resolve(/* loaded component */)
12+
})
13+
})
14+
// ... use `AsyncComp` like a normal component
2015
```
2116

22-
As you can see, this method accepts a factory function returning a `Promise`. Promise's `resolve` callback should be called when you have retrieved your component definition from the server. You can also call `reject(reason)` to indicate the load has failed.
17+
As you can see, this method accepts a loader function that returns a Promise. The Promise's `resolve` callback should be called when you have retrieved your component definition from the server. You can also call `reject(reason)` to indicate the load has failed.
2318

24-
You can also return a `Promise` in the factory function, so with Webpack 2 or later and ES2015 syntax you can do:
19+
[ES module dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports) also returns a Promise, so most of the time we will use it in combination with `defineAsyncComponent`. Bundlers like Vite and webpack also support the syntax, so we can use it to import Vue SFCs:
2520

2621
```js
2722
import { defineAsyncComponent } from 'vue'
2823

2924
const AsyncComp = defineAsyncComponent(() =>
30-
import('./components/AsyncComponent.vue')
25+
import('./components/MyComponent.vue')
3126
)
32-
33-
app.component('async-component', AsyncComp)
3427
```
3528

29+
The resulting `AsyncComp` is a wrapper component that only calls the loader function when it is actually rendered on the page. In addition, it will pass along any props to the inner component, so you can use the async wrapper to seamlessly replace the original component while achieving lazy loading.
30+
31+
<div class="options-api">
32+
3633
You can also use `defineAsyncComponent` when [registering a component locally](/guide/components/registration.html#local-registration):
3734

3835
```js
39-
import { createApp, defineAsyncComponent } from 'vue'
36+
import { defineAsyncComponent } from 'vue'
4037

41-
createApp({
38+
export default {
4239
// ...
4340
components: {
4441
AsyncComponent: defineAsyncComponent(() =>
4542
import('./components/AsyncComponent.vue')
4643
)
4744
}
48-
})
45+
}
4946
```
5047

51-
## Using with Suspense
48+
</div>
49+
50+
## Loading State
5251

53-
Async components are _suspensible_ by default. This means if it has a `<Suspense>` in the parent chain, it will be treated as an async dependency of that `<Suspense>`. In this case, the loading state will be controlled by the `<Suspense>`, and the component's own loading, error, delay and timeout options will be ignored.
52+
## Error Handling
5453

55-
The async component can opt-out of `Suspense` control and let the component always control its own loading state by specifying `suspensible: false` in its options.
54+
## Using with Suspense
5655

57-
You can check the list of available options in the [API Reference](/api/general.html#defineasynccomponent)
56+
Async components can be used with the `<Suspense>` built-in component. The interaction between `<Suspense>` and async components are documented in the [dedicated chapter for `<Suspense>`](/guide/built-ins/suspense.html).

0 commit comments

Comments
 (0)