最好的书籍设计网站,家政公司,正泰营销云,如何进入公司网站后台作用#xff1a;监视数据的变化#xff08;和Vue2中的watch作用一致#xff09;特点#xff1a;Vue3中的watch只能监视以下四种数据#xff1a; ref定义的数据。reactive定义的数据。函数返回一个值#xff08;getter函数#xff09;。一个包含上述内容的数组。 我们在V…作用监视数据的变化和Vue2中的watch作用一致特点Vue3中的watch只能监视以下四种数据 ref定义的数据。reactive定义的数据。函数返回一个值getter函数。一个包含上述内容的数组。 我们在Vue3中使用watch的时候通常会遇到以下几种情况
ref定义的基本类型
templatediv classpersonh2当前求和{{ sum }}/h2button clickchangeAddOne点击加一/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect } from vue// 数据
let sum ref(0);// 方法
function changeAddOne() {sum.value;
}// 监视 watch(谁回调函数)
watch(sum, (newValue, oldValue) {console.log(sum发生变化了, newValue, oldValue);
});
/scriptwatch监视里面的ref数据不需要.value。
主动停止监视
templatediv classpersonh2当前求和{{ sum }}/h2button clickchangeAddOne点击加一/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect } from vue// 数据
let sum ref(0);// 方法
function changeAddOne() {sum.value;
}// 监视 watch(谁回调函数)
const stopWatch watch(sum, (newValue, oldValue) {console.log(sum发生变化了, newValue, oldValue);if(newValue 10) {stopWatch();}
});
/scriptref定义的对象数据
监视ref定义的【对象类型】数据直接写数据名监视的是对象的【地址值】若想监视对象内部的数据要手动开启深度监视。 注意 若修改的是ref定义的对象中的属性newValue 和 oldValue 都是新值因为它们是同一个对象。若修改整个ref定义的对象newValue 是新值 oldValue 是旧值因为不是同一个对象了。 templatediv classpersonh2Person/h2p姓名{{ person.name }}/pp年龄{{ person.age }}/pbutton clickchangeAge年龄1/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect } from vue// 数据
let person ref({name: 张三,age: 18
});
// 方法
function changeAge() {person.value.age;
}// 监视 watch(谁回调函数)
// 监视的是对象的地址值若想监视对象内部的属性需要手动开启深度监视 - deep: true
watch(person, (newVal, oldVal) {console.log(watch:, newVal, oldVal);
}, {deep: true, immediate: true});
/scriptwatch的参数
第一个参数被监视的数据第二个参数监视的回调第三个监视配置对象(deep, imediate)
reactive定义的对象数据
reactive默认监视开启深度监视。
templatediv classpersonh2Person/h2p姓名{{ person.name }}/pp年龄{{ person.age }}/pbutton clickchangeAge年龄1/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect, reactive } from vue// 数据
let person reactive({name: 张三,age: 18
});
// 方法
function changeAge() {person.age;
}// 监视 watch(谁回调函数)
// 默认开始深度监视
watch(person, (newVal, oldVal) {console.log(watch:, newVal, oldVal);
}, {immediate: true});
/script监视ref/reactive定义的对象类型中某一个属性
监视ref或reactive定义的【对象类型】数据中的某个属性注意点如下
若该属性值不是【对象类型】需要写成函数形式。若该属性值是依然是【对象类型】可直接编也可写成函数建议写成函数。
结论监视的要是对象里的属性那么最好写函数式注意点若是对象监视的是地址值需要关注对象内部需要手动开启深度监视。
templatediv classpersonh2Person/h2p姓名{{ person.name }}/pp年龄{{ person.age }}/pbutton clickchangeAge年龄1/buttonp车{{ person.car.c1 }}/pbutton clickchangeCarC1修改第一量车/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect, reactive } from vue// 数据
let person reactive({name: 张三,age: 18,car:{c1:宝马,c2:奔驰}
});
// 方法
function changeAge() {person.age;
}
function changeCarC1() {person.car.c1 ---;
}
// 监视 watch(谁回调函数)
// 默认开始深度监视
watch(() {return person.age
}, (newVal, oldVal) {console.log(age:, newVal, oldVal);
});
/scripttemplatediv classpersonh2Person/h2p姓名{{ person.name }}/pp年龄{{ person.age }}/pbutton clickchangeAge年龄1/buttonp车{{ person.car.c1 }}/pbutton clickchangeCarC1修改第一量车/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect, reactive } from vue// 数据
let person reactive({name: 张三,age: 18,car:{c1:宝马,c2:奔驰}
});
// 方法
function changeAge() {person.age;
}
function changeCarC1() {person.car.c1 ---;
}
// 监视 watch(谁回调函数)
// 默认开始深度监视
watch(() {return person.car
}, (newVal, oldVal) {console.log(car:, newVal, oldVal);
}, {deep:true});
/script多个数据
templatediv classpersonh2Person/h2p姓名{{ person.name }}/pp年龄{{ person.age }}/pbutton clickchangeAge年龄1/buttonp车{{ person.car.c1 }}/pbutton clickchangeCarC1修改第一量车/button/div
/templatescript langts setup namePerson
import { computed,watch, ref, watchEffect, reactive } from vue// 数据
let person reactive({name: 张三,age: 18,car:{c1:宝马,c2:奔驰}
});
// 方法
function changeAge() {person.age;
}
function changeCarC1() {person.car.c1 ---;
}
// 监视 watch(谁回调函数)
// 默认开始深度监视
watch(() {return [person.car, person.age]
}, (newVal, oldVal) {console.log(car:, newVal, oldVal);
}, {deep:true});
/script
文章转载自: http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.kwcnf.cn.gov.cn.kwcnf.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.fhbhr.cn.gov.cn.fhbhr.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.snbq.cn.gov.cn.snbq.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.qdscb.cn.gov.cn.qdscb.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn