watch监听
侦听 ref 和 reactive
const state = reactive({ count: 0 });
// 侦听reactive里面属性需要包裹在箭头函数对其返回
watch(
() => state.count,
(count, prevCount) => {
/* ... */
}
);
// watch直接接受 ref 作为监听对象,并在回调函数返回解包后的值,同样的解包操作也发生在模板和reactive中
const count = ref(0);
watch(count, (count, prevCount) => {
/* ... */
});
如果我们想侦听 props 上的属性变化
watch(
() => props.msg,
(newVal, oldVal) => {
console.log(newVal, oldVal);
}
);
基于最新的 Vite / TypeScript / Vue3 / ElementPlus打造的前后端一体管理后台框架,免费开源,可以快速开发企业级应用
演示地址:https://demo.eleadmin.cn/