コンテンツにスキップ

useMutationObserver

カテゴリー
エクスポートサイズ
613 B
最終更新日
先週

DOMツリーに加えられた変更を監視します。MutationObserver MDN

デモ

使い方

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.dokyumento.jp/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
丶远方
Jelf
Anthony Fu
Pim
jdm1219
chirokas
huodoushigemi
AaronBeaudoin
laozei
karma
Shinigami
Alex Kozack
Antério Vieira
zhong666

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2のサポートを終了し、バンドルを最適化してクリーンアップしました (#4349)
v11.0.0-beta.1 2024/06/12
489a9 - 修正: クリーンアップの前に監視を停止する (#4037)
v10.8.0 2024年2月20日
98fac - 機能: 複数のターゲットを許可する (#3741)
v10.6.0 2023年11月9日
f9136 - 機能: takeRecords 関数を追加 (#3480)
v10.5.0 2023年10月7日
8167b - 機能: MaybeComputedElementRef を使用 (#3430)

MITライセンスの下でリリースされています。