Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/onElementRemoval.md
1---2category: Sensors3---45# onElementRemoval67Fires when the element or any element containing it is removed from the DOM.89## Usage1011```vue {13}12<script setup lang="ts">13import { onElementRemoval } from '@vueuse/core'14import { shallowRef, useTemplateRef } from 'vue'1516const btnRef = useTemplateRef('btn')17const btnState = shallowRef(true)18const removedCount = shallowRef(0)1920function btnOnClick() {21btnState.value = !btnState.value22}2324onElementRemoval(btnRef, () => ++removedCount.value)25</script>2627<template>28<button29v-if="btnState"30@click="btnOnClick"31>32recreate me33</button>34<button35v-else36ref="btnRef"37@click="btnOnClick"38>39remove me40</button>41<b>removed times: {{ removedCount }}</b>42</template>43```4445### Callback with Mutation Records4647The callback receives an array of `MutationRecord` objects that triggered the removal.4849```ts50import { onElementRemoval } from '@vueuse/core'5152onElementRemoval(targetRef, (mutationRecords) => {53console.log('Element removed', mutationRecords)54})55```5657### Return Value5859Returns a stop function to stop observing.6061```ts62const stop = onElementRemoval(targetRef, callback)6364// Later, stop observing65stop()66```6768## Type Declarations6970```ts71export interface OnElementRemovalOptions72extends73ConfigurableWindow,74ConfigurableDocumentOrShadowRoot,75WatchOptionsBase {}76/**77* Fires when the element or any element containing it is removed.78*79* @param target80* @param callback81* @param options82*/83export declare function onElementRemoval(84target: MaybeElementRef,85callback: (mutationRecords: MutationRecord[]) => void,86options?: OnElementRemovalOptions,87): Fn88```89