在Vue 3中,可以使用watch API来监听响应式元素的变化。
例如,如果有一个响应式元素count,可以使用以下代码来监听它的变化:
import { watch, reactive } from 'vue'
const state = reactive({
count: 0
})
watch(() => state.count, (newValue, oldValue) => {
console.log(`count changed from ${oldValue} to ${newValue}`)
})
在上面的代码中,我们使用watch函数来监听state.count的变化。当state.count发生变化时,回调函数将被触发,并打印出变化前后的值。
可以使用类似的方法来监听其他响应式元素的变化。












评论(0)