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

Vue 3 app.unmount() Function

Vue 3 app.unmount() Function


Vue3 Global API Vue 3 Global API

In Vue 3,app.unmount() is used to unmount (i.e., destroy) an already-mounted Vue application instance.

app.unmount() is a very useful function in Vue 3 that allows you to safely destroy a Vue application instance and release related resources.


1. What is the app.unmount()?

app.unmount() is a global API provided by Vue 3 for unmounting an already-mounted Vue application instance. When you call this function, Vue performs the following operations:

Simply put,app.unmount() removes the entire Vue application from the page and releases all related resources.


2. Why Do We Need app.unmount()?

In certain scenarios, you may need to dynamically destroy a Vue application instance. For example:


3. How to Use app.unmount()?

Use app.unmount() is very simple; here is a basic usage example:

Example

import

{

createApp

}

from

'vue'

;

import

App from

'./App.vue'

;

// create Vue application instance

const

app

=

createApp

(

App

)

;

// mount the Vue application

app.

mount

(

'#app'

)

;

// unmount the Vue application

app.

unmount

(

)

;

Code analysis:

  1. Create application instance: First, we used createApp() function to create a Vue application instance and passed in the root component App.
  2. Mount application: Then, we called app.mount('#app') to mount the application onto the page's #app element.
  3. Unmount the application: Finally, we called app.unmount() to unmount the application from the page.

Notes:


4. Lifecycle Hooks

When calling app.unmount() , Vue executes the lifecycle hooks in the following order:

  1. beforeUnmount: Called before the component instance is destroyed; suitable for performing cleanup operations.
  2. unmounted: Called after the component instance is destroyed, when all event listeners and child components have already been removed.

Example

export

default

{

  beforeUnmount

(

)

{

    console.

log

(

'Component is about to be destroyed'

)

;

 

}

,

  unmounted

(

)

{

    console.

log

(

'The component has been destroyed'

)

;

 

}

}


Vue3 Global API Vue 3 Global API