onDeactivated()
Runs when a KeepAlive-cached component is removed from the DOM.
Syntax
onDeactivated(callback): void Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | () => void | No | Function run when the cached component is hidden. |
Returns
void — Nothing; registers the hook.
Examples
<script setup lang="ts">
import { onActivated, onDeactivated } from 'vue';
let id: number;
onActivated(() => {
id = window.setInterval(() => {}, 1000);
});
onDeactivated(() => clearInterval(id));
</script>
Notes
Fires only for components inside <KeepAlive> when they are switched away but
kept in memory (not destroyed). Use it to pause timers, polling, or
animations, resuming them in onActivated. onUnmounted still runs when the
cache itself is destroyed.