- 监听对象中的某个属性
// 监听demo对象的name属性
watch(() => demo.name, (newValue, oldValue) => {
console.log('watch 已触发', newValue)
})
- 只监听对象的子属性
watch(() => ({ ...demo }), (newValue, oldValue) => {
console.log('watch 已触发', newValue)
})
这种情况,只有当 demo 的子属性发生变更时才会触发 watch 方法。孙属性,曾孙属性… 发生变更都不会触发 watch 方法。也就是说,当你修改 demo.soulmate.name 或者 demo.soulmate.nickName 时是不会触发 watch 方法的。