Vue 3 Lifecycle Hooks
Lifecycle hook functions in Vue.js are a series of methods that Vue automatically calls during the creation, update, and destruction of a component instance. In these steps, the component triggers a series of functions called "lifecycle hooks," allowing developers to execute custom code at specific moments.
Registration and Usage of Lifecycle Hooks
mounted hook: This hook is triggered after the component finishes its initial render and generates DOM nodes. For example:
export default {
mounted() {
console.log(`组件已挂载。`);
}
}
Other common hooks: besides mounted, there are hooks such as updated and unmounted, which are called at different lifecycle stages of the instance.
Notes on Lifecycle Hooks
**this context:** In all lifecycle hook functions, this automatically points to the component instance that called it.
**Avoid arrow functions:** When defining lifecycle hooks, do not use arrow functions, because this would make it impossible to access the component instance via this.
By using lifecycle hooks reasonably, developers can execute specific logic at different stages of the component, enhancing its flexibility and functionality.
Below is the diagram of the component instance lifecycle:

The following are Vue.js lifecycle hook functions
| Hook Name | Trigger Timing |
|---|---|
beforeCreate |
Called after instance initialization, before data observation and event configuration. |
created |
Called immediately after the instance is created; at this point the instance has completed data observation, property and method computation, and watch/event callbacks. |
beforeMount |
Called before mounting begins: the related render function is called for the first time. |
mounted |
el Replaced by the newly created vm.$el and called after it is mounted onto the instance. |
beforeUpdate |
Called when data is updated, before the virtual DOM is re-rendered and patched. |
updated |
Called after the virtual DOM re-render and patching caused by data changes. |
beforeUnmount |
Called before the instance is unmounted. |
unmounted |
Called after the instance is unmounted. |
The following is a detailed introduction to the lifecycle hooks of Vue components:
Lifecycle hook functions in Vue.js are a series of methods that Vue automatically calls during the creation, update, and destruction of a component instance. Through these hook functions, you can perform specific operations at different lifecycle stages of the component. The following is a detailed introduction to the lifecycle hooks of Vue components:
1. beforeCreate
- Description: Called after instance initialization, before data observation and event configuration.
- Usage: You can initialize some data not proxied by Vue at this stage.
Example:
Example
beforeCreate
(
)
{
console.log
(
'beforeCreate
:
Instance just created
)
;
}
2. created
- Description: Called immediately after the instance is created; at this point the instance has completed the following configuration: data observation, computation of properties and methods, and watch/event callbacks. The mount phase has not yet started.
- Usage: You can access and modify data at this stage, but the DOM is not yet mounted.
Example:
Example
created
(
)
{
console.log
(
'created
:
Instance created
)
;
}
3. beforeMount
- Description: Called before mounting begins: the related render function is called for the first time.
- Usage: Perform some operations before mounting, such as modifying the DOM structure.
Example:
Example
beforeMount
(
)
{
console.log
(
'beforeMount
:
Before mount begins
)
;
}
4. mounted
- Description:
elReplaced by the newly createdvm.$eland called after it is mounted onto the instance. - Usage: You can manipulate the DOM and execute DOM-related logic at this stage.
Example:
Example
mounted
(
)
{
console.log
(
'mounted
:
Instance mounted
)
;
}
5. beforeUpdate
- Description: Called when data is updated, before the virtual DOM is re-rendered and patched.
- Usage: You can access the state before the update at this stage.
Example:
Example
beforeUpdate
(
)
{
console.log
(
'beforeUpdate
:
Before data update
)
;
}
6. updated
- Description: Called after the virtual DOM re-render and patching caused by data changes.
- Usage: You can manipulate the DOM at this stage, but avoid changing data during this period, as it may cause an infinite update loop.
Example:
Example
updated
(
)
{
console.log
(
'updated
:
Data update complete
)
;
}
7. beforeUnmount (Vue 3)
- Description: Called before the instance is unmounted.
- Usage: Perform some cleanup operations before the instance is unmounted, such as removing event listeners.
Example:
Example
beforeUnmount
(
)
{
console.log
(
'beforeUnmount
:
Before instance unmount
)
;
}
8. unmounted (Vue 3)
- Description: Called after the instance is unmounted.
- Usage: Perform some cleanup operations after the instance is unmounted, such as destroying timers.
Example:
Example
unmounted
(
)
{
console.log
(
'unmounted
:
Instance unmounted
)
;
}
Lifecycle Hook Execution Order
- beforeCreate
- created
- beforeMount
- mounted
- beforeUpdate (triggered when data changes)
- updated (triggered when data changes)
- beforeUnmount
- unmounted
Example
The following is a complete Vue component example demonstrating the use of all lifecycle hooks:
Example
const
LifecycleDemo
=
{
template
:
`
<div{{message}}div`,data(){return{message>:
'Hello
,
Vue
!
'
}
;
}
,
beforeCreate
(
)
{
console.log
(
'beforeCreate
:
Instance just created
)
;
}
,
created
(
)
{
console.log
(
'created
:
Instance created
)
;
}
,
beforeMount
(
)
{
console.log
(
'beforeMount
:
Before mount begins
)
;
}
,
mounted
(
)
{
console.log
(
'mounted
:
Instance mounted
)
;
}
,
beforeUpdate
(
)
{
console.log
(
'beforeUpdate
:
Before data update
)
;
}
,
updated
(
)
{
console.log
(
'updated
:
Data update complete
)
;
}
,
beforeUnmount
(
)
{
console.log
(
'beforeUnmount
:
Before instance unmount
)
;
}
,
unmounted
(
)
{
console.log
(
'unmounted
:
Instance unmounted
)
;
}
}
;
const
app
=
Vue.createApp
(
{
components
:
{
LifecycleDemo
}
}
)
;
app.mount
(
'
app'
)
;
Try it »
This example prints a log for each lifecycle hook function to the console, helping you understand the order and purpose of each lifecycle stage.

Composition API Hooks
Vue 3 introduced the Composition API (Composition API), through setup functions, you can organize lifecycle hooks more clearly in logic. When using the Composition API, these hooks are defined via onXxx functions:
onMounted: equivalent tomounted.onBeforeUnmount: equivalent tobeforeUnmount.onUnmounted: equivalent tounmounted.onBeforeMount: equivalent tobeforeMount.onBeforeUpdate: equivalent tobeforeUpdate.onUpdated: equivalent toupdated.onCreatedandonBeforeCreateis usually not commonly used in the Composition API, becausesetup's code runs during the creation phase.
Using Composition API lifecycle hooks:
Example
import
{
onMounted
,
onBeforeUnmount
}
from
'vue'
;
export
default
{
setup
(
)
{
onMounted
(
(
)
=>
{
console.
log
(
'Component mounted!'
)
;
// perform initialization here
}
)
;
onBeforeUnmount
(
(
)
=>
{
console.
log
(
'Component about to unmount!'
)
;
// perform cleanup here
}
)
;
}
}
;
Composition API advantages:
- Strong reusability: Lifecycle hooks can be combined with other logic, making encapsulation and reuse easier.
- Clearer logic: Together with
setup's other logic in a single function, reducing code fragmentation and complexity.