1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| // attr表示需要监视的属性 // 情况一:监视单个ref定义的响应式数据 watch(attr,(newValue,oldValue)=>{ console.log('attr变化了',newValue,oldValue); })
// 情况二; 监视多个ref定义的响应式数据 watch([attr1,attr2,....,attrn],(newValue,oldValue)=>{ console.log('attr1或attrn变化了',newValue,oldValue); })
// obj表示需要监听的对象 // 情况三:监视reactive定义的响应式数据 // 若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue // 若watch监视的是reactive定义的响应式数据,则强制打开开启了深度监视 watch(obj,(newValue,oldValue)=>{ console.log('obj变化了',newValue,oldValue)
},{immediate:true,deep:false}); // 此处deep配置不在奏效
// 情况四,监视reactive定义的响应式数据中的某个属性 watch(()=>person.job,(newValue,oldValue)=>{ console.log('person的job变化了',newValue,oldValue) },{immediate:true,deep:true})
// 情况五:监视reactive定义的响应式数据中的某一些属性 watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{ console.log('person的job变化了',newValue,oldValue) }) // 特殊情况 watch(()=>person.job,(newValue,oldValue)=>{ console.log('person的job变化了',newValue,oldValue) },{deep:false});// 此处由于是监视reactive所定义的对象中的某个属性,所以deep配置有效
|