From 995b72dbce63eb2461259324bb16af3019c2cc3a Mon Sep 17 00:00:00 2001 From: ntepluhina Date: Mon, 10 Feb 2020 17:18:49 +0200 Subject: [PATCH 01/13] Added dynamic components --- src/.vuepress/components/dynamic-1.vue | 93 +++++++++++++++ src/.vuepress/components/dynamic-2.vue | 95 +++++++++++++++ .../components/tab-posts-dynamic.vue | 57 +++++++++ src/.vuepress/config.js | 4 +- src/guide/component-dynamic-async.md | 111 ++++++++++++++++++ 5 files changed, 359 insertions(+), 1 deletion(-) create mode 100644 src/.vuepress/components/dynamic-1.vue create mode 100644 src/.vuepress/components/dynamic-2.vue create mode 100644 src/.vuepress/components/tab-posts-dynamic.vue create mode 100644 src/guide/component-dynamic-async.md diff --git a/src/.vuepress/components/dynamic-1.vue b/src/.vuepress/components/dynamic-1.vue new file mode 100644 index 0000000000..7f079b841c --- /dev/null +++ b/src/.vuepress/components/dynamic-1.vue @@ -0,0 +1,93 @@ + + + + + diff --git a/src/.vuepress/components/dynamic-2.vue b/src/.vuepress/components/dynamic-2.vue new file mode 100644 index 0000000000..12734b51b3 --- /dev/null +++ b/src/.vuepress/components/dynamic-2.vue @@ -0,0 +1,95 @@ + + + + + diff --git a/src/.vuepress/components/tab-posts-dynamic.vue b/src/.vuepress/components/tab-posts-dynamic.vue new file mode 100644 index 0000000000..d820ce0e6e --- /dev/null +++ b/src/.vuepress/components/tab-posts-dynamic.vue @@ -0,0 +1,57 @@ + + + + + diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js index aee406177d..8b50761da8 100644 --- a/src/.vuepress/config.js +++ b/src/.vuepress/config.js @@ -49,7 +49,9 @@ module.exports = { children: [ 'component-registration', 'component-props', - 'component-custom-events' + 'component-custom-events', + // 'component-slots', + 'component-dynamic-async' ] } ] diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md new file mode 100644 index 0000000000..88cd11ba7a --- /dev/null +++ b/src/guide/component-dynamic-async.md @@ -0,0 +1,111 @@ +# Dynamic & Async Components + +> This page assumes you've already read the [Components Basics](components.md). Read that first if you are new to components. + +## `keep-alive` with Dynamic Components + +Earlier, we used the `is` attribute to switch between components in a tabbed interface: + +```html + +``` + +When switching between these components though, you'll sometimes want to maintain their state or avoid re-rendering for performance reasons. For example, when expanding our tabbed interface a little: + + + +You'll notice that if you select a post, switch to the _Archive_ tab, then switch back to _Posts_, it's no longer showing the post you selected. That's because each time you switch to a new tab, Vue creates a new instance of the `currentTabComponent`. + +Recreating dynamic components is normally useful behavior, but in this case, we'd really like those tab component instances to be cached once they're created for the first time. To solve this problem, we can wrap our dynamic component with a `` element: + +```html + + + + +``` + +Check out the result below: + + + +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. + +:::tip Note +Note that `` requires the components being switched between to all have names, either using the `name` option on a component, or through local/global registration +::: + +Check out more details on `` in the [API reference](TODO:../api/#keep-alive). + +## Async Components + +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: + +```js +Vue.component('async-example', function(resolve, reject) { + setTimeout(function() { + // Pass the component definition to the resolve callback + resolve({ + template: '
I am async!
' + }) + }, 1000) +}) +``` + +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/): + +```js +Vue.component('async-webpack-example', function(resolve) { + // This special require syntax will instruct Webpack to + // automatically split your built code into bundles which + // are loaded over Ajax requests. + require(['./my-async-component'], resolve) +}) +``` + +You can also return a `Promise` in the factory function, so with Webpack 2 and ES2015 syntax you can do: + +```js +Vue.component( + 'async-webpack-example', + // The `import` function returns a Promise. + () => import('./my-async-component') +) +``` + +When using [local registration](components-registration.html#Local-Registration), you can also directly provide a function that returns a `Promise`: + +```js +new Vue({ + // ... + components: { + 'my-component': () => import('./my-async-component') + } +}) +``` + +

If you're a Browserify user that would like to use async components, its creator has unfortunately [made it clear](https://github.com/substack/node-browserify/issues/58#issuecomment-21978224) that async loading "is not something that Browserify will ever support." Officially, at least. The Browserify community has found [some workarounds](https://github.com/vuejs/vuejs.org/issues/620), which may be helpful for existing and complex applications. For all other scenarios, we recommend using Webpack for built-in, first-class async support.

+ +### Handling Loading State + +> New in 2.3.0+ + +The async component factory can also return an object of the following format: + +```js +const AsyncComponent = () => ({ + // The component to load (should be a Promise) + component: import('./MyComponent.vue'), + // A component to use while the async component is loading + loading: LoadingComponent, + // A component to use if the load fails + error: ErrorComponent, + // Delay before showing the loading component. Default: 200ms. + delay: 200, + // The error component will be displayed if a timeout is + // provided and exceeded. Default: Infinity. + timeout: 3000 +}) +``` + +> Note that you must use [Vue Router](https://github.com/vuejs/vue-router) 2.4.0+ if you wish to use the above syntax for route components. From 00aa12eca3c83cfc1642e85a2dd9004752f677fa Mon Sep 17 00:00:00 2001 From: ntepluhina Date: Sun, 16 Feb 2020 15:31:11 +0200 Subject: [PATCH 02/13] Finished dynamic components --- src/guide/component-dynamic-async.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 88cd11ba7a..23da09417c 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -29,7 +29,7 @@ Check out the result below: -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. +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. :::tip Note Note that `` 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 `` in the [API reference](TODO:../api/#kee 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: ```js -Vue.component('async-example', function(resolve, reject) { - setTimeout(function() { +const app = Vue.createApp({}) + +app.component('async-example', (resolve, reject) => { + setTimeout(() => { // Pass the component definition to the resolve callback resolve({ template: '
I am async!
' @@ -55,7 +57,7 @@ Vue.component('async-example', function(resolve, reject) { 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/): ```js -Vue.component('async-webpack-example', function(resolve) { +app.component('async-webpack-example', resolve => { // This special require syntax will instruct Webpack to // automatically split your built code into bundles which // are loaded over Ajax requests. From 0c06b406bb8d918c4475143bc715cd9d2d9195e0 Mon Sep 17 00:00:00 2001 From: Natalia Tepluhina Date: Wed, 18 Mar 2020 08:36:46 +0200 Subject: [PATCH 03/13] Update src/guide/component-dynamic-async.md Co-Authored-By: Rahul Kadyan --- src/guide/component-dynamic-async.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 23da09417c..b13eb920fc 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -6,7 +6,7 @@ Earlier, we used the `is` attribute to switch between components in a tabbed interface: -```html +```vue-html ``` From 4c9763ab363b285b10b399b4426b2eb1692b7120 Mon Sep 17 00:00:00 2001 From: Natalia Tepluhina Date: Wed, 18 Mar 2020 08:36:54 +0200 Subject: [PATCH 04/13] Update src/guide/component-dynamic-async.md Co-Authored-By: Rahul Kadyan --- src/guide/component-dynamic-async.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index b13eb920fc..744e5c2ad8 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -18,7 +18,7 @@ You'll notice that if you select a post, switch to the _Archive_ tab, then switc Recreating dynamic components is normally useful behavior, but in this case, we'd really like those tab component instances to be cached once they're created for the first time. To solve this problem, we can wrap our dynamic component with a `` element: -```html +```vue-html From 508d46ce76802be0f4749a31f46f98e7fd33ba4d Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Sun, 5 Apr 2020 17:03:28 +0300 Subject: [PATCH 05/13] fix: added dynamic components link to sidebar --- src/.vuepress/config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js index ea8ba642f8..e756f08e45 100644 --- a/src/.vuepress/config.js +++ b/src/.vuepress/config.js @@ -26,6 +26,7 @@ const sidebar = { '/guide/component-custom-events', '/guide/component-slots', '/guide/component-provide-inject', + '/guide/component-dynamic-async', ], }, { From ccfb8e71b9f90dedf1cc9037d73a056c06d99911 Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Sun, 5 Apr 2020 19:16:42 +0300 Subject: [PATCH 06/13] fix: refactored dynamic components --- src/.vuepress/components/dynamic-1.vue | 93 ------------------ src/.vuepress/components/dynamic-2.vue | 95 ------------------- .../components/tab-posts-dynamic.vue | 57 ----------- src/guide/component-dynamic-async.md | 26 +++-- 4 files changed, 18 insertions(+), 253 deletions(-) delete mode 100644 src/.vuepress/components/dynamic-1.vue delete mode 100644 src/.vuepress/components/dynamic-2.vue delete mode 100644 src/.vuepress/components/tab-posts-dynamic.vue diff --git a/src/.vuepress/components/dynamic-1.vue b/src/.vuepress/components/dynamic-1.vue deleted file mode 100644 index 7f079b841c..0000000000 --- a/src/.vuepress/components/dynamic-1.vue +++ /dev/null @@ -1,93 +0,0 @@ - - - - - diff --git a/src/.vuepress/components/dynamic-2.vue b/src/.vuepress/components/dynamic-2.vue deleted file mode 100644 index 12734b51b3..0000000000 --- a/src/.vuepress/components/dynamic-2.vue +++ /dev/null @@ -1,95 +0,0 @@ - - - - - diff --git a/src/.vuepress/components/tab-posts-dynamic.vue b/src/.vuepress/components/tab-posts-dynamic.vue deleted file mode 100644 index d820ce0e6e..0000000000 --- a/src/.vuepress/components/tab-posts-dynamic.vue +++ /dev/null @@ -1,57 +0,0 @@ - - - - - diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 744e5c2ad8..21c40be426 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -12,7 +12,12 @@ Earlier, we used the `is` attribute to switch between components in a tabbed int When switching between these components though, you'll sometimes want to maintain their state or avoid re-rendering for performance reasons. For example, when expanding our tabbed interface a little: - +

+ See the Pen + Dynamic components: without keep-alive by Vue (@Vue) + on CodePen. +

+ You'll notice that if you select a post, switch to the _Archive_ tab, then switch back to _Posts_, it's no longer showing the post you selected. That's because each time you switch to a new tab, Vue creates a new instance of the `currentTabComponent`. @@ -27,9 +32,14 @@ Recreating dynamic components is normally useful behavior, but in this case, we' Check out the result below: - +

+ See the Pen + Dynamic components: with keep-alive by Vue (@Vue) + on CodePen. +

+ -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. +Now the _Posts_ tab maintains its state (the selected post) even when it's not rendered. :::tip Note Note that `` requires the components being switched between to all have names, either using the `name` option on a component, or through local/global registration @@ -48,7 +58,7 @@ app.component('async-example', (resolve, reject) => { setTimeout(() => { // Pass the component definition to the resolve callback resolve({ - template: '
I am async!
' + template: '
I am async!
', }) }, 1000) }) @@ -57,7 +67,7 @@ app.component('async-example', (resolve, reject) => { 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/): ```js -app.component('async-webpack-example', resolve => { +app.component('async-webpack-example', (resolve) => { // This special require syntax will instruct Webpack to // automatically split your built code into bundles which // are loaded over Ajax requests. @@ -81,8 +91,8 @@ When using [local registration](components-registration.html#Local-Registration) new Vue({ // ... components: { - 'my-component': () => import('./my-async-component') - } + 'my-component': () => import('./my-async-component'), + }, }) ``` @@ -106,7 +116,7 @@ const AsyncComponent = () => ({ delay: 200, // The error component will be displayed if a timeout is // provided and exceeded. Default: Infinity. - timeout: 3000 + timeout: 3000, }) ``` From ba0d03fe2f82710697fcea8dd2826a4688f44f05 Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Sun, 5 Apr 2020 21:23:32 +0300 Subject: [PATCH 07/13] fix: updated anvanced usage --- src/guide/component-dynamic-async.md | 79 +++++++++++++++------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 21c40be426..168693b108 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -49,75 +49,82 @@ Check out more details on `` in the [API reference](TODO:../api/#kee ## Async Components -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: +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: ```js const app = Vue.createApp({}) -app.component('async-example', (resolve, reject) => { - setTimeout(() => { - // Pass the component definition to the resolve callback - resolve({ - template: '
I am async!
', +const AsyncComp = Vue.defineAsyncComponent( + () => + new Promise((resolve, reject) => { + resolve({ + template: '
I am async!
', + }) }) - }, 1000) -}) -``` - -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/): +) -```js -app.component('async-webpack-example', (resolve) => { - // This special require syntax will instruct Webpack to - // automatically split your built code into bundles which - // are loaded over Ajax requests. - require(['./my-async-component'], resolve) -}) +app.component('async-example', AsyncComp) ``` +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. + You can also return a `Promise` in the factory function, so with Webpack 2 and ES2015 syntax you can do: ```js -Vue.component( - 'async-webpack-example', - // The `import` function returns a Promise. - () => import('./my-async-component') +import { defineAsyncComponent } from 'vue' + +const AsyncComp = defineAsyncComponent(() => + import('./components/AsyncComponent.vue') ) + +app.component('async-component', AsyncComp) ``` When using [local registration](components-registration.html#Local-Registration), you can also directly provide a function that returns a `Promise`: ```js -new Vue({ +import { createApp, defineAsyncComponent } from 'vue' + +createApp({ // ... components: { - 'my-component': () => import('./my-async-component'), + components: { + AsyncComponent: defineAsyncComponent(() => + import('./components/AsyncComponent.vue') + ), + }, }, }) ``` -

If you're a Browserify user that would like to use async components, its creator has unfortunately [made it clear](https://github.com/substack/node-browserify/issues/58#issuecomment-21978224) that async loading "is not something that Browserify will ever support." Officially, at least. The Browserify community has found [some workarounds](https://github.com/vuejs/vuejs.org/issues/620), which may be helpful for existing and complex applications. For all other scenarios, we recommend using Webpack for built-in, first-class async support.

- -### Handling Loading State +### Advanced usage -> New in 2.3.0+ - -The async component factory can also return an object of the following format: +The `defineAsyncComponent` method can also return an object of the following format: ```js -const AsyncComponent = () => ({ - // The component to load (should be a Promise) - component: import('./MyComponent.vue'), +import { defineAsyncComponent } from 'vue' + +const AsyncComp = defineAsyncComponent({ + // The factory function + loader: () => import('./Foo.vue') // A component to use while the async component is loading - loading: LoadingComponent, + loadingComponent: LoadingComponent, // A component to use if the load fails - error: ErrorComponent, + errorComponent: ErrorComponent, // Delay before showing the loading component. Default: 200ms. delay: 200, // The error component will be displayed if a timeout is // provided and exceeded. Default: Infinity. timeout: 3000, + // A function that returns a boolean indicating whether the async component should retry when the loader promise rejects + retryWhen: error => error.code !== 404, + // Maximum allowed retries number + maxRetries: 3, + // Defining if component is suspensible + suspensible: false }) ``` -> Note that you must use [Vue Router](https://github.com/vuejs/vue-router) 2.4.0+ if you wish to use the above syntax for route components. +Async components are _suspensible_ by default. This means if it has a [](TODO) in the parent chain, it will be treated as an async dependency of that ``. In this case, the loading state will be controlled by the ``, and the component's own loading, error, delay and timeout options will be ignored. + +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. From a5137cdaa35f9da145957ddeb1ee95daaddaf901 Mon Sep 17 00:00:00 2001 From: Natalia Tepluhina Date: Tue, 7 Apr 2020 21:37:10 +0300 Subject: [PATCH 08/13] Update src/guide/component-dynamic-async.md Co-Authored-By: Rahul Kadyan --- src/guide/component-dynamic-async.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 168693b108..d2ad4a674b 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -125,6 +125,6 @@ const AsyncComp = defineAsyncComponent({ }) ``` -Async components are _suspensible_ by default. This means if it has a [](TODO) in the parent chain, it will be treated as an async dependency of that ``. In this case, the loading state will be controlled by the ``, and the component's own loading, error, delay and timeout options will be ignored. +Async components are _suspensible_ by default. This means if it has a [``](TODO) in the parent chain, it will be treated as an async dependency of that ``. In this case, the loading state will be controlled by the ``, and the component's own loading, error, delay and timeout options will be ignored. 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. From 3ecfe9c55447514b6705227b22b57d5f1167a8ef Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Sat, 11 Apr 2020 17:30:04 +0300 Subject: [PATCH 09/13] fix: fixed keep-alive naming and explanation --- src/guide/component-dynamic-async.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 168693b108..d84362e432 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -2,7 +2,7 @@ > This page assumes you've already read the [Components Basics](components.md). Read that first if you are new to components. -## `keep-alive` with Dynamic Components +## Dynamic Components with `keep-alive` Earlier, we used the `is` attribute to switch between components in a tabbed interface: @@ -42,7 +42,7 @@ Check out the result below: Now the _Posts_ tab maintains its state (the selected post) even when it's not rendered. :::tip Note -Note that `` requires the components being switched between to all have names, either using the `name` option on a component, or through local/global registration +When you're switching between components, `` requires them all to have names. This should be done either using the `name` option on a component, or through local/global registration ::: Check out more details on `` in the [API reference](TODO:../api/#keep-alive). @@ -58,7 +58,7 @@ const AsyncComp = Vue.defineAsyncComponent( () => new Promise((resolve, reject) => { resolve({ - template: '
I am async!
', + template: '
I am async!
' }) }) ) @@ -91,9 +91,9 @@ createApp({ components: { AsyncComponent: defineAsyncComponent(() => import('./components/AsyncComponent.vue') - ), - }, - }, + ) + } + } }) ``` From 4da392860d64bdca6acb09d86ef23f43b8cbbb6e Mon Sep 17 00:00:00 2001 From: Natalia Tepluhina Date: Tue, 14 Apr 2020 08:53:40 +0300 Subject: [PATCH 10/13] Update src/guide/component-dynamic-async.md Co-Authored-By: Rahul Kadyan --- src/guide/component-dynamic-async.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 4006a1b890..68b121b952 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -68,7 +68,7 @@ app.component('async-example', AsyncComp) 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. -You can also return a `Promise` in the factory function, so with Webpack 2 and ES2015 syntax you can do: +You can also return a `Promise` in the factory function, so with Webpack 2 or later and ES2015 syntax you can do: ```js import { defineAsyncComponent } from 'vue' From a5a1617ba2da4f3fba2a84126ce162c81dc0416c Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Tue, 14 Apr 2020 15:50:44 +0300 Subject: [PATCH 11/13] fix: fixed component local registration --- src/guide/component-dynamic-async.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 4006a1b890..527976ae5d 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -80,7 +80,7 @@ const AsyncComp = defineAsyncComponent(() => app.component('async-component', AsyncComp) ``` -When using [local registration](components-registration.html#Local-Registration), you can also directly provide a function that returns a `Promise`: +You can also use `defineAsyncComponent` when [registering a component locally](components-registration.html#Local-Registration): ```js import { createApp, defineAsyncComponent } from 'vue' @@ -88,11 +88,9 @@ import { createApp, defineAsyncComponent } from 'vue' createApp({ // ... components: { - components: { - AsyncComponent: defineAsyncComponent(() => - import('./components/AsyncComponent.vue') - ) - } + AsyncComponent: defineAsyncComponent(() => + import('./components/AsyncComponent.vue') + ) } }) ``` From c4408737b5025b82b9ba0488bb1b01f0edf3d0ff Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Tue, 14 Apr 2020 15:54:40 +0300 Subject: [PATCH 12/13] fix: removed advanced usage --- src/guide/component-dynamic-async.md | 30 +++------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index d1439f9f82..71cab5b41a 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -95,34 +95,10 @@ createApp({ }) ``` -### Advanced usage - -The `defineAsyncComponent` method can also return an object of the following format: - -```js -import { defineAsyncComponent } from 'vue' - -const AsyncComp = defineAsyncComponent({ - // The factory function - loader: () => import('./Foo.vue') - // A component to use while the async component is loading - loadingComponent: LoadingComponent, - // A component to use if the load fails - errorComponent: ErrorComponent, - // Delay before showing the loading component. Default: 200ms. - delay: 200, - // The error component will be displayed if a timeout is - // provided and exceeded. Default: Infinity. - timeout: 3000, - // A function that returns a boolean indicating whether the async component should retry when the loader promise rejects - retryWhen: error => error.code !== 404, - // Maximum allowed retries number - maxRetries: 3, - // Defining if component is suspensible - suspensible: false -}) -``` +### Using with Suspense Async components are _suspensible_ by default. This means if it has a [``](TODO) in the parent chain, it will be treated as an async dependency of that ``. In this case, the loading state will be controlled by the ``, and the component's own loading, error, delay and timeout options will be ignored. 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. + +You can check the list of available options in the [API Reference](TODO) From cac81e3f9d3236b0e1a4e6c92f7624c291844df7 Mon Sep 17 00:00:00 2001 From: NataliaTepluhina Date: Tue, 14 Apr 2020 16:03:38 +0300 Subject: [PATCH 13/13] fix: removed keep-alive note --- src/guide/component-dynamic-async.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md index 71cab5b41a..adce11fc9a 100644 --- a/src/guide/component-dynamic-async.md +++ b/src/guide/component-dynamic-async.md @@ -41,10 +41,6 @@ Check out the result below: Now the _Posts_ tab maintains its state (the selected post) even when it's not rendered. -:::tip Note -When you're switching between components, `` requires them all to have names. This should be done either using the `name` option on a component, or through local/global registration -::: - Check out more details on `` in the [API reference](TODO:../api/#keep-alive). ## Async Components