Vue3 defineAsyncComponent() Function
defineAsyncComponent() is a function introduced in Vue 3 for asynchronously loading components. It allows you to load a component only when needed, rather than loading all components at application startup. This is very useful for optimizing the performance of large applications and reducing initial load time.
Why Use defineAsyncComponent()?
When developing large applications, if all components are loaded at startup, the initial load time may become too long and affect user experience. By using defineAsyncComponent(), you can lazily load certain components, loading them only when the user navigates to the relevant page or feature, thereby improving application performance.
How to Use defineAsyncComponent()?
Basic Usage
To use defineAsyncComponent(), you need to import it from Vue and pass it to the component that needs to be loaded asynchronously. Here is a simple example:
Example
import
{
defineAsyncComponent
}
from
'vue'
;
const
AsyncComponent
=
defineAsyncComponent
(
(
)
=>
import
(
'./components/MyComponent.vue'
)
)
;
export
default
{
components
:
{
AsyncComponent
}
}
;
In this example,MyComponent.vue The component will be loaded asynchronously when needed.
Handling Loading States
defineAsyncComponent() also allows you to define loading states, such as loading and load-failed. You can configure these states by passing an object:
Example
const
AsyncComponent
=
defineAsyncComponent
(
{
loader
:
(
)
=>
import
(
'./components/MyComponent.vue'
)
,
loadingComponent
:
LoadingComponent
,
// Component shown while loading
errorComponent
:
ErrorComponent
,
// Component shown when loading fails
delay
:
200
,
// Delay before showing the loading component (milliseconds)
timeout
:
3000
// Loading timeout (milliseconds)
}
)
;
In this configuration,LoadingComponent is displayed while the component is loading,ErrorComponent is displayed when loading fails.
Error handling
If component loading fails,defineAsyncComponent() provides a onError callback function to handle errors:
Example
const
AsyncComponent
=
defineAsyncComponent
(
{
loader
:
(
)
=>
import
(
'./components/MyComponent.vue'
)
,
onError
(
error
,
retry
,
fail
,
attempts
)
{
if
(
error.
message
.
includes
(
'Failed to fetch dynamically imported module'
)
)
{
retry
(
)
;
// Retry loading
}
else
{
fail
(
)
;
// Abort loading
}
}
}
)
;
In this example, if loading fails, you can choose to retry or abort loading.
Practical Application Scenarios
Load Routes on Demand
In Vue Router, you can use defineAsyncComponent() to load route components on demand:
Example
import
{
defineAsyncComponent
}
from
'vue'
;
const
routes
=
[
{
path
:
'/about'
,
component
:
defineAsyncComponent
(
(
)
=>
import
(
'./views/AboutView.vue'
)
)
}
]
;
This way,AboutView.vue the component is only loaded when the user visits the /about path.
Lazy-Load Large Components
For some large components, you can use defineAsyncComponent() to defer loading and reduce initial load time:
Example
const
LargeComponent
=
defineAsyncComponent
(
(
)
=>
import
(
'./components/LargeComponent.vue'
)
)
;
Vue 3 Global API