Vue3 Watch监听函数
tep1:引入watch函数和toRaw方法
import { watch, toRaw } from "vue"
tep2:监听
// 监听函数,监听formState对象,对象类型监听要开启deep深度监听
watch(
formState,
(newData, oldData) => {
console.log(toRaw(newData), toRaw(oldData));//toRaw()为取到proxy对象中的数据
},
{
deep: true,
}
);
tep3:监听对象中某一属性的变化时需要改为回调函数
watch(
() => formState.value.user,
() => console.log(formState.value.user)
);