Async Components
Basic Usage
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
function:
As you can see,
defineAsyncComponent
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.
ES module dynamic import
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 (and will use it as bundle split points), so we can use it to import Vue SFCs:
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 and slots to the inner component, so you can use the async wrapper to seamlessly replace the original component while achieving lazy loading.
As with normal components, async components can be
registered globally
using
app.component()
:
They can also be defined directly inside their parent component:
Loading and Error States
Asynchronous operations inevitably involve loading and error states -
defineAsyncComponent()
supports handling these states via advanced options: