diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js index 1791f38fc2..e756f08e45 100644 --- a/src/.vuepress/config.js +++ b/src/.vuepress/config.js @@ -14,8 +14,8 @@ const sidebar = { '/guide/list', '/guide/events', '/guide/forms', - '/guide/component-basics' - ] + '/guide/component-basics', + ], }, { title: 'Components In-Depth', @@ -25,20 +25,21 @@ const sidebar = { '/guide/component-props', '/guide/component-custom-events', '/guide/component-slots', - '/guide/component-provide-inject' - ] + '/guide/component-provide-inject', + '/guide/component-dynamic-async', + ], }, { title: 'Migration to Vue 3', collapsable: true, - children: ['migration'] + children: ['migration'], }, { title: 'Contribute to the Docs', collapsable: true, - children: ['writing-guide'] - } - ] + children: ['writing-guide'], + }, + ], } module.exports = { @@ -50,9 +51,9 @@ module.exports = { { href: 'https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', - rel: 'stylesheet' - } - ] + rel: 'stylesheet', + }, + ], ], themeConfig: { nav: [ @@ -62,8 +63,8 @@ module.exports = { items: [ { text: 'Guide', link: '/guide/introduction' }, { text: 'Style Guide', link: '/style-guide/' }, - { text: 'Tooling', link: '/tooling/' } - ] + { text: 'Tooling', link: '/tooling/' }, + ], }, { text: 'API Reference', link: '/api/' }, { @@ -71,8 +72,8 @@ module.exports = { ariaLabel: 'Examples Menu', items: [ { text: 'Examples', link: '/examples/' }, - { text: 'Cookbook', link: '/cookbook/' } - ] + { text: 'Cookbook', link: '/cookbook/' }, + ], }, { text: 'Community', @@ -80,16 +81,16 @@ module.exports = { items: [ { text: 'Team', link: '/community/team/' }, { text: 'Partners', link: '/community/partners/' }, - { text: 'Themes', link: '/community/themes/' } - ] - } + { text: 'Themes', link: '/community/themes/' }, + ], + }, ], sidebarDepth: 2, sidebar: { '/guide/': sidebar.guide, - '/community/': sidebar.guide + '/community/': sidebar.guide, }, - smoothScroll: false + smoothScroll: false, }, plugins: { '@vuepress/pwa': { @@ -97,17 +98,17 @@ module.exports = { updatePopup: { '/': { message: 'New content is available.', - buttonText: 'Refresh' - } - } - } + buttonText: 'Refresh', + }, + }, + }, }, markdown: { /** @param {import('markdown-it')} md */ - extendMarkdown: md => { + extendMarkdown: (md) => { md.options.highlight = require('./markdown/highlight')( md.options.highlight ) - } - } + }, + }, } diff --git a/src/guide/component-dynamic-async.md b/src/guide/component-dynamic-async.md new file mode 100644 index 0000000000..adce11fc9a --- /dev/null +++ b/src/guide/component-dynamic-async.md @@ -0,0 +1,100 @@ +# Dynamic & Async Components + +> This page assumes you've already read the [Components Basics](components.md). Read that first if you are new to components. + +## Dynamic Components with `keep-alive` + +Earlier, we used the `is` attribute to switch between components in a tabbed interface: + +```vue-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: + +

+ 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`. + +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: + +```vue-html + + + + +``` + +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. + +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 possible, Vue has a `defineAsyncComponent` method: + +```js +const app = Vue.createApp({}) + +const AsyncComp = Vue.defineAsyncComponent( + () => + new Promise((resolve, reject) => { + resolve({ + template: '
I am async!
' + }) + }) +) + +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 or later and ES2015 syntax you can do: + +```js +import { defineAsyncComponent } from 'vue' + +const AsyncComp = defineAsyncComponent(() => + import('./components/AsyncComponent.vue') +) + +app.component('async-component', AsyncComp) +``` + +You can also use `defineAsyncComponent` when [registering a component locally](components-registration.html#Local-Registration): + +```js +import { createApp, defineAsyncComponent } from 'vue' + +createApp({ + // ... + components: { + AsyncComponent: defineAsyncComponent(() => + import('./components/AsyncComponent.vue') + ) + } +}) +``` + +### 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)