watch:既要指明监听的属性,也要指明监听的回调。
watchEffect:不需要知名监听哪个属性,监听回调中用到哪个属性,就监听哪个属性。
watchEffect与computed:
computed 注重计算出来的值(回调函数的返回值),所以必须写返回值;
watchEffect 注重的是过程(回调函数的函数体),所以不用写返回值。
用法:
因为用到了 sum 和 personperson.job.j1.salay,所以当页面上这两个属性被修改时,才会触发watchEffect。
watchEffect(() => {// 因为用到了sum和personperson.job.j1.salay,所以当页面上这两个属性被修改时,才会触发watchEffectconst x1 = sum.valueconst x2 = person.job.j1.salayconsole.log('watchEffect执行了');
})