watchArray
追加と削除を伴う配列を監視します。
使用方法
watch
と似ていますが、コールバック関数に追加および削除された要素を提供します。リストが`push`、`splice`などでその場で更新される場合は、`{ deep: true }`を渡します。
ts
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]
})
型宣言
typescript
export declare type WatchArrayCallback<V = any, OV = any> = (
value: V,
oldValue: OV,
added: V,
removed: OV,
onCleanup: (cleanupFn: () => void) => void,
) => any
/**
* Watch for an array with additions and removals.
*
* @see https://vueuse.dokyumento.jp/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<Immediate>,
): WatchHandle
ソース
貢献者
変更履歴
v12.0.0-beta.1
2024/11/21