主题
useMutationObserver
观察对 DOM 树所做的更改。MDN 突变观察者
¥Watch for changes being made to the DOM tree. MutationObserver MDN
示例
用法
¥Usage
ts
import { useMutationObserver } from '@vueuse/core'
import { ref } from 'vue'
export default {
setup() {
const el = ref(null)
const messages = ref([])
useMutationObserver(el, (mutations) => {
if (mutations[0])
messages.value.push(mutations[0].attributeName)
}, {
attributes: true,
})
return {
el,
messages,
}
},
}
类型声明
typescript
export interface UseMutationObserverOptions
extends MutationObserverInit,
ConfigurableWindow {}
/**
* Watch for changes being made to the DOM tree.
*
* @see https://vueuse.org/useMutationObserver
* @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver MutationObserver MDN
* @param target
* @param callback
* @param options
*/
export declare function useMutationObserver(
target:
| MaybeComputedElementRef
| MaybeComputedElementRef[]
| MaybeRefOrGetter<MaybeElement[]>,
callback: MutationCallback,
options?: UseMutationObserverOptions,
): {
isSupported: ComputedRef<boolean>
stop: () => void
takeRecords: () => MutationRecord[] | undefined
}
export type UseMutationObserverReturn = ReturnType<typeof useMutationObserver>