Skip to content

Commit 972b85d

Browse files
ntepluhinaNataliaTepluhina
ntepluhina
authored andcommitted
Finished dynamic components
1 parent 7d2ef76 commit 972b85d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/guide/component-dynamic-async.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Check out the result below:
2929

3030
<dynamic-2/>
3131

32-
Now the _Posts_ tab maintains its state (the selected post) even when it's not rendered. See [this example](https://codesandbox.io/s/github/vuejs/vuejs.org/tree/master/src/v2/examples/vue-20-keep-alive-with-dynamic-components) for the complete code.
32+
Now the _Posts_ tab maintains its state (the selected post) even when it's not rendered. See [this example](https://codesandbox.io/s/components-keep-alive-f6k3r) for the complete code.
3333

3434
:::tip Note
3535
Note that `<keep-alive>` requires the components being switched between to all have names, either using the `name` option on a component, or through local/global registration
@@ -42,8 +42,10 @@ Check out more details on `<keep-alive>` in the [API reference](TODO:../api/#kee
4242
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 easier, Vue allows you to define your component as a factory function that asynchronously resolves your component definition. Vue will only trigger the factory function when the component needs to be rendered and will cache the result for future re-renders. For example:
4343

4444
```js
45-
Vue.component('async-example', function(resolve, reject) {
46-
setTimeout(function() {
45+
const app = Vue.createApp({})
46+
47+
app.component('async-example', (resolve, reject) => {
48+
setTimeout(() => {
4749
// Pass the component definition to the resolve callback
4850
resolve({
4951
template: '<div>I am async!</div>'
@@ -55,7 +57,7 @@ Vue.component('async-example', function(resolve, reject) {
5557
As you can see, the factory function receives a `resolve` callback, which 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. The `setTimeout` here is for demonstration; how to retrieve the component is up to you. One recommended approach is to use async components together with [Webpack's code-splitting feature](https://webpack.js.org/guides/code-splitting/):
5658

5759
```js
58-
Vue.component('async-webpack-example', function(resolve) {
60+
app.component('async-webpack-example', resolve => {
5961
// This special require syntax will instruct Webpack to
6062
// automatically split your built code into bundles which
6163
// are loaded over Ajax requests.

0 commit comments

Comments
 (0)