考虑以下(简化的(存储。它持有一个";注释";(类对象(:
// store.ts
import { reactive } from 'vue'
import { Note } from 'src/lib/note'
class Store {
allNotes: Note[] = []
}
export const store = reactive(new Store())
我想看allNotes
的变化:
// note.ts
import { store } from 'src/lib/store'
watch(store.allNotes,
(oldVal, newVal) => _.difference(newVal, oldVal)
.forEach(note => {
console.log("note changed")
}),
{deep: true}
)
当更改特定音符(store.allNotes
元素的属性(时,我确实在Chome DevTools Vue中看到了更改,但我的手表不会触发。为什么?
reactive
属性在watch方法中不会自动展开(与ref
属性不同(,因此您需要使用函数watch(()=>store.allNotes,...
:返回它们
watch(()=>store.allNotes,
(oldVal, newVal) => _.difference(newVal, oldVal)
.forEach(note => {
console.log("note changed")
}),
{deep: true}
)