Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Lifecycle Hooks

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

Example:

Example

beforeCreate

(

)

{

  console.log

(

'beforeCreate

:

Instance just created

)

;

}

2. created

Example:

Example

created

(

)

{

  console.log

(

'created

:

Instance created

)

;

}

3. beforeMount

Example:

Example

beforeMount

(

)

{

  console.log

(

'beforeMount

:

Before mount begins

)

;

}

4. mounted

Example:

Example

mounted

(

)

{

  console.log

(

'mounted

:

Instance mounted

)

;

}

5. beforeUpdate

Example:

Example

beforeUpdate

(

)

{

  console.log

(

'beforeUpdate

:

Before data update

)

;

}

6. updated

Example:

Example

updated

(

)

{

  console.log

(

'updated

:

Data update complete

)

;

}

7. beforeUnmount (Vue 3)

Example:

Example

beforeUnmount

(

)

{

  console.log

(

'beforeUnmount

:

Before instance unmount

)

;

}

8. unmounted (Vue 3)

Example:

Example

unmounted

(

)

{

  console.log

(

'unmounted

:

Instance unmounted

)

;

}

Lifecycle Hook Execution Order

  1. beforeCreate
  2. created
  3. beforeMount
  4. mounted
  5. beforeUpdate (triggered when data changes)
  6. updated (triggered when data changes)
  7. beforeUnmount
  8. 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:

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: