onElementRemoval ​
Fires when the element or any element containing it is removed.
Demo ​
demo1: recreate new element
removed times: 0
demo2: reuse same element
target elementremoved times: 0
Usage ​
vue
<script setup lang="ts">
import { onElementRemoval } from '@vueuse/core'
import { ref } from 'vue'
const btnRef = ref<HTMLElement | null>(null)
const btnState = ref(true)
const removedCount = ref(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>
Type Declarations ​
typescript
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
Source ​
Contributors ​
Ben Lau