Skip to content
On this page

watchArray

观察添加和删除的数组。

用法

类似于watch,但为回调函数提供添加和删除的元素。如果列表用pushsplice等更新到位,则传递{deep:true}

ts
import { watchArray } from '@mpxjs/mpxuse-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]
})
import { watchArray } from '@mpxjs/mpxuse-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]
})

类型声明

typescript
/**
 * Watch for an array with additions and removals.
 *
 * @see https://mpxuse.cn/watchArray
 */
export declare function watchArray<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T[]> | T[],
  cb: WatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>,
  options?: WatchOptions
): () => void
/**
 * Watch for an array with additions and removals.
 *
 * @see https://mpxuse.cn/watchArray
 */
export declare function watchArray<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T[]> | T[],
  cb: WatchArrayCallback<T[], Immediate extends true ? T[] | undefined : T[]>,
  options?: WatchOptions
): () => void

源码

源码文档

贡献者

pagnkelly

更新日志

No recent changes

根据MIT许可证发布。