Vue3中watch监听写法

Felix_Openmind / 2023-05-18 / 原文


侦听ref和reactive

const state = reactive({
	count: 0
})

// 侦听reactive中属性,需要包裹在箭头函数中
watch(() => state.count, (newVal, oldVal) => {
	
})

// watch直接接受ref作为监听对象,并且在回调函数返回解包后的值
const count = ref(0);
watch(count, (newVal, oldVal) => {
	
})

// 监听props上属性的变化
watch(() => props.msg, (newVal, oldVal) => {
	console.log(newVal, oldVal);
})