onUnmounted()
Registers a callback to run after the component is unmounted.
Syntax
onUnmounted(callback): void Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | () => void | No | Cleanup function run when the component is destroyed. |
Returns
void — Nothing; registers the hook.
Examples
<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue';
let id: number;
onMounted(() => {
id = window.setInterval(() => console.log('tick'), 1000);
});
onUnmounted(() => clearInterval(id));
</script>
Notes
Runs after the component is removed from the DOM and all child components are
unmounted. Use it to clear timers, remove event listeners, and dispose
subscriptions or third-party instances created in onMounted.