Files
fuel-price/.agents/skills/vueuse-functions/references/onElementRemoval.md
Ovidiu U 4a3ce4cc1d
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled
docs: add advanced skills for Vitest, Pinia, and Vue built-ins
Add comprehensive reference documentation for:
- Vitest: environments, projects/workspaces, type testing, vi utilities
- Pinia: HMR, Nuxt integration, SSR setup
- Vue: built-in components (Transition, Teleport, Suspense, KeepAlive) and advanced directives
2026-04-11 16:28:36 +01:00

89 lines
1.6 KiB
Markdown

---
category: Sensors
---
# onElementRemoval
Fires when the element or any element containing it is removed from the DOM.
## Usage
```vue {13}
<script setup lang="ts">
import { onElementRemoval } from '@vueuse/core'
import { shallowRef, useTemplateRef } from 'vue'
const btnRef = useTemplateRef('btn')
const btnState = shallowRef(true)
const removedCount = shallowRef(0)
function btnOnClick() {
btnState.value = !btnState.value
}
onElementRemoval(btnRef, () => ++removedCount.value)
</script>
<template>
<button
v-if="btnState"
@click="btnOnClick"
>
recreate me
</button>
<button
v-else
ref="btnRef"
@click="btnOnClick"
>
remove me
</button>
<b>removed times: {{ removedCount }}</b>
</template>
```
### Callback with Mutation Records
The callback receives an array of `MutationRecord` objects that triggered the removal.
```ts
import { onElementRemoval } from '@vueuse/core'
onElementRemoval(targetRef, (mutationRecords) => {
console.log('Element removed', mutationRecords)
})
```
### Return Value
Returns a stop function to stop observing.
```ts
const stop = onElementRemoval(targetRef, callback)
// Later, stop observing
stop()
```
## Type Declarations
```ts
export interface OnElementRemovalOptions
extends
ConfigurableWindow,
ConfigurableDocumentOrShadowRoot,
WatchOptionsBase {}
/**
* Fires when the element or any element containing it is removed.
*
* @param target
* @param callback
* @param options
*/
export declare function onElementRemoval(
target: MaybeElementRef,
callback: (mutationRecords: MutationRecord[]) => void,
options?: OnElementRemovalOptions,
): Fn
```