Skip to content

useMutationObserver

类别
导出体积
558 B
上次更改
last month

监视对 DOM 树的变更。MutationObserver MDN

示例

用法

vue
<script setup lang="ts">
import { 
useMutationObserver
} from '@vueuse/core'
import {
ref
,
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
('el')
const
messages
=
ref
([])
useMutationObserver
(
el
, (
mutations
) => {
if (
mutations
[0])
messages
.
value
.
push
(
mutations
[0].
attributeName
)
}, {
attributes
: true,
}) </script> <template> <
div
ref
="
el
">
Hello VueUse </
div
>
</template>

类型声明

ts
export interface UseMutationObserverOptions
  extends MutationObserverInit,
    ConfigurableWindow {}
/**
 * 监视对 DOM 树的变更
 *
 * @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
>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
IlyaL
Anthony Fu
丶远方
Jelf
Issayah
青椒肉丝
我想静静
OrbisK
Pim
jdm1219
chirokas
huodoushigemi
AaronBeaudoin
laozei
karma
Shinigami
Alex Kozack
Antério Vieira
zhong666

更新日志

7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
021d0 - feat(toArray): new utility function (#4432)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
489a9 - fix: stop watching before cleaning up (#4037)
98fac - feat: allow multiple targets (#3741)
f9136 - feat: add takeRecords function (#3480)
8167b - feat: use MaybeComputedElementRef (#3430)