与vue2中computed配置功能一致
变化:需要引入(组合式的API)
姓 :
名 :
全名:{{ person.fullName }}
变化:需要引入(组合式的API)
watch(sum,(newValue,oldValue)=>{
console.log(newValue,oldValue);
//立即监听
},{immediate:true})
watch([sum,msg],(newValue,oldValue)=>{
console.log('sum或msg变了',newValue,oldValue);
})
watch(person,(newValue,oldValue)=>{
console.log('person变化了',newValue,oldValue);
})
watch(()=> person.name),(newValue,oldValue)=>{
console.log('name变化了',newValue,oldValue);
})
watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
console.log('name和age变化了',newValue,oldValue);
})
特殊情况:
watch(()=>person.job,(newValue,oldValue)=>{
console.log('person的job变化了',newValue,oldValue)
},{deep:true})
此处由于监视的是reactive定义的对象中的某个属性,所以deep配置有效
变化:需要引入(组合式的API
watch的套路是:既要指明监视的属性,也要指明监视的回调
watchEffect的套路是:不用指明监视哪个属性,监视的回调中用到哪个属性,就监视哪个属性
watchEffect有点像computed:
watchEffect(()=>{
const x1 = sum.value
const x2 = person.age
console.log('watchEffect配置的回调执行了')
})