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

Vue 3 app.onUnmount() Function

Vue 3 app.onUnmount() Function Explained


Vue3 Global API Vue 3 Global API

app.onUnmount() is an API for adding a callback function that executes when a component is unmounted.

app.onUnmount() is a very useful API in Vue 3 that helps developers perform necessary cleanup when a component is unmounted, avoiding memory leaks and resource waste. By using app.onUnmount()properly, you can improve the application's stability and performance.


1. What is the app.onUnmount()?

app.onUnmount() is a global API provided by Vue 3 that allows developers to add a callback function to the application instance, which is automatically executed when a component is unmounted. This feature is especially suitable for cleaning up resources, removing event listeners, or performing other operations that need to be handled when the component is unmounted.


2. Basic Syntax

Example

app.

onUnmount

(

(

)

=>

{

 

// write the code to execute when the component is unmounted here

}

)

;

app.onUnmount() accepts a callback function as a parameter, which is automatically called when the component is unmounted.


3. Use Cases

3.1 Clean Up Resources

In a component, we may create some resources that need to be cleaned up when the component is unmounted, such as timers, WebSocket connections, or third-party library instances. Using app.onUnmount() ensures these resources are properly cleaned up when the component is unmounted.

Example

app.

onUnmount

(

(

)

=>

{

  clearInterval

(

timerID

)

;

// clear timer

  webSocket.

close

(

)

;

// close WebSocket connection

}

)

;

3.2 Remove Event Listeners

If a component registers global event listeners, to avoid memory leaks, those listeners should be removed when the component is unmounted.

Example

app.

onUnmount

(

(

)

=>

{

  window.

removeEventListener

(

'resize'

,

handleResize

)

;

// remove window resize event listener

}

)

;

3.3 Cancel Subscriptions

In a component, we may subscribe to some data sources or event streams, such as RxJS Observables. When the component is unmounted, those subscriptions should be cancelled to avoid unnecessary resource consumption.

Example

app.

onUnmount

(

(

)

=>

{

  subscription.

unsubscribe

(

)

;

// cancel subscription

}

)

;


4. Code Example

Here is a complete code example showing how to use app.onUnmount() function in Vue 3.

Example

import

{

createApp

}

from

'vue'

;

const

app

=

createApp

(

{

  setup

(

)

{

   

const

timerID

=

setInterval

(

(

)

=>

{

      console.

log

(

'Timer is running...'

)

;

   

}

,

1000

)

;

    app.

onUnmount

(

(

)

=>

{

      clearInterval

(

timerID

)

;

// clean up timer when component is unmounted

      console.

log

(

'Timer has been cleared.'

)

;

   

}

)

;

   

return

{

}

;

 

}

}

)

;

app.

mount

(

'#app'

)

;

In this example, we created a timer that outputs "Timer is running..." once per second. When the component is unmounted, the timer is cleared and "Timer has been cleared." is output.


5. Notes