Vue3 侦听器有下列写法:

  1. 监听单个数据变化:
watch: {
  count: {
    handler(newVal, oldVal) {
      console.log('count changed:', newVal, oldVal)
    },
    immediate: true
  }
}
  1. 监听多个数据变化:
watch: {
  count(newVal, oldVal) {
    console.log('count changed:', newVal, oldVal)
  },
  message(newVal, oldVal) {
    console.log('message changed:', newVal, oldVal)
  }
}
  1. 监听对象属性变化:
watch: {
  userInfo: {
    handler(newVal, oldVal) {
      console.log('userInfo changed:', newVal, oldVal)
    },
    deep: true
  }
}
  1. 监听嵌套对象属性变化:
watch: {
  'userInfo.name': {
    handler(newVal, oldVal) {
      console.log('userInfo.name changed:', newVal, oldVal)
    }
  }
}
  1. 监听计算属性变化:
watch: {
  fullName(newVal, oldVal) {
    console.log('fullName changed:', newVal, oldVal)
  }
}
vue3 侦听器有哪些写法

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

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