watchArray

watchArray

配列の追加と削除を監視します。

使用法

watch と似ていますが、コールバック関数に追加された要素と削除された要素を提供します。pushsplice などでリストがその場で更新される場合は、{ deep: true } を渡してください。

import { watchArray } from '@vueuse/core'

const list = ref([1, 2, 3])

watchArray(list, (newList, oldList, added, removed) => {
  console.log(newList) // [1, 2, 3, 4]
  console.log(oldList) // [1, 2, 3]
  console.log(added) // [4]
  console.log(removed) // []
})

onMounted(() => {
  list.value = [...list.value, 4]
})