是的,在Vue 3中,watchEffect可以接受多个响应式变量作为参数。当其中任何一个变量发生变化时,watchEffect函数都会被重新执行。例如:

import { watchEffect, reactive } from 'vue'

const state = reactive({
  count1: 0,
  count2: 0,
})

watchEffect(() => {
  console.log(`Count1: ${state.count1}`)
  console.log(`Count2: ${state.count2}`)
})

state.count1++ // 输出 "Count1: 1"
state.count2++ // 输出 "Count2: 1"

在上面的例子中,我们使用watchEffect监听了state对象中的count1和count2两个属性。当任何一个属性发生变化时,watchEffect中的函数都会被重新执行,并输出相应的计数值。

vue3中watchEffect可以写多个吗

原文地址: http://www.cveoy.top/t/topic/h8uQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录