vue3中watch监听不同数据源的方法总结

 更新时间:2026年03月26日 08:57:47   作者:送鱼的老默  
这篇文章主要为大家详细介绍了vue3中watch监听不同数据源的常用方法,文中的示例代码讲解详细,具有一定的借鉴价值,大家可以根据自己的需要进行选择

在 Vue 3 中,watch 监听不同数据源的方式有所不同

1. 监听单个 ref

import { ref, watch } from 'vue'

const count = ref(0)

// 直接传入 ref
watch(count, (newVal, oldVal) => {
  console.log('count 变化:', newVal, oldVal)
})

// 或者使用 getter 函数
watch(() => count.value, (newVal, oldVal) => {
  console.log('count 变化:', newVal, oldVal)
})

2. 监听多个 ref

import { ref, watch } from 'vue'

const count = ref(0)
const name = ref('John')

// 方式1:使用数组
watch([count, name], ([newCount, newName], [oldCount, oldName]) => {
  console.log('count 变化:', newCount, oldCount)
  console.log('name 变化:', newName, oldName)
})

// 方式2:使用 getter 数组
watch(
  [() => count.value, () => name.value],
  ([newCount, newName], [oldCount, oldName]) => {
    console.log('多个数据变化')
  }
)

3. 监听单个 reactive

import { reactive, watch } from 'vue'

const state = reactive({
  count: 0,
  name: 'John'
})

// ❌ 错误:直接传入 reactive 对象无法监听到内部属性的变化
watch(state, (newVal, oldVal) => {
  console.log('不会触发') // 深度监听时才会触发
})

// ✅ 正确:使用 getter 函数监听特定属性
watch(
  () => state.count,
  (newVal, oldVal) => {
    console.log('count 变化:', newVal, oldVal)
  }
)

// ✅ 深度监听整个 reactive 对象
watch(
  () => state,
  (newVal, oldVal) => {
    console.log('state 任何属性变化都会触发')
  },
  { deep: true }
)

4. 监听多个 reactive 数据

import { reactive, watch } from 'vue'

const state1 = reactive({ count: 0 })
const state2 = reactive({ name: 'John' })

// 方式1:使用 getter 数组
watch(
  [() => state1.count, () => state2.name],
  ([newCount, newName], [oldCount, oldName]) => {
    console.log('数据变化')
  }
)

// 方式2:深度监听整个 reactive 对象(不推荐)
watch(
  [() => state1, () => state2],
  ([newState1, newState2], [oldState1, oldState2]) => {
    // 注意:oldState1 和 newState1 指向同一个对象
    console.log('状态变化')
  },
  { deep: true }
)

5. 混合监听 ref 和 reactive

import { ref, reactive, watch } from 'vue'

const count = ref(0)
const state = reactive({ name: 'John', age: 18 })

watch(
  [count, () => state.name, () => state.age],
  ([newCount, newName, newAge], [oldCount, oldName, oldAge]) => {
    console.log('混合数据变化')
  }
)

6. 监听响应式对象的属性

import { reactive, watch } from 'vue'

const user = reactive({
  info: {
    name: 'John',
    address: {
      city: 'Beijing'
    }
  }
})

// 监听嵌套属性
watch(
  () => user.info.address.city,
  (newVal, oldVal) => {
    console.log('城市变化:', newVal, oldVal)
  }
)

// 深度监听整个对象
watch(
  () => user,
  (newVal, oldVal) => {
    console.log('user 任何变化')
  },
  { deep: true }
)

总结对比

数据源监听方式注意事项
单个 refwatch(count, callback)直接传入即可
多个 refwatch([ref1, ref2], callback)使用数组形式
单个 reactive 属性watch(() => state.prop, callback)必须使用 getter
多个 reactive 属性watch([() => state.prop1, () => state.prop2], callback)使用 getter 数组
整个 reactivewatch(() => state, callback, { deep: true })必须深度监听

最佳实践建议

  • 优先使用 getter 函数,特别是监听 reactive 对象的属性
  • 避免深度监听大型对象,可能会影响性能
  • 注意旧值的引用问题:对于 reactive 对象,旧值可能与新值相同(因为引用未变)

到此这篇关于vue3中watch监听不同数据源的方法总结的文章就介绍到这了,更多相关vue3 watch监听数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue3+Vue Router实现动态路由导航的示例代码

    Vue3+Vue Router实现动态路由导航的示例代码

    随着单页面应用程序(SPA)的日益流行,前端开发逐渐向复杂且交互性强的方向发展,在这个过程中,Vue.js及其生态圈的工具(如Vue Router)为我们提供了强大的支持,本文将介绍如何在Vue 3中使用Vue Router实现动态路由导航,需要的朋友可以参考下
    2024-08-08
  • 解决vue3项目中el-menu不兼容SSR问题

    解决vue3项目中el-menu不兼容SSR问题

    这篇文章主要介绍了解决vue3项目中el-menu不兼容SSR问题,需要的朋友可以参考下
    2023-12-12
  • 基于Vue过渡状态实例讲解

    基于Vue过渡状态实例讲解

    下面小编就为大家带来一篇基于Vue过渡状态实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Vue自定义指令深入探讨实现

    Vue自定义指令深入探讨实现

    这篇文章主要介绍了Vue自定义指令的实现,Vue支持自定义指令,开发者可以根据自己的需求,创建自己的指令来扩展Vue的功能,需要详细了解可以参考下文
    2023-05-05
  • 在Vue项目中使用d3.js的实例代码

    在Vue项目中使用d3.js的实例代码

    这篇文章主要介绍了在Vue项目中使用d3.js的实例代码,非常不错,具有参考借鉴价值价值,需要的朋友可以参考下
    2018-05-05
  • VUE3安装element ui失败的原因以及解决办法

    VUE3安装element ui失败的原因以及解决办法

    这篇文章主要给大家介绍了关于VUE3安装element ui失败的原因以及解决的相关资料,很多朋友升级使用Vue3了,但在安装element ui失败出错了,这里给大家总结下,需要的朋友可以参考下
    2023-09-09
  • Element-UI el-calendar样式如何修改日历

    Element-UI el-calendar样式如何修改日历

    这篇文章主要介绍了Element-UI el-calendar样式如何修改日历问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Vue3+Ts+Vite项目websoket封装使用方式

    Vue3+Ts+Vite项目websoket封装使用方式

    文章介绍了如何封装WebSocket并进行页面使用,包括安装npm、创建websocket.ts文件、配置请求地址、全局类型声明以及在页面中引用和注册
    2025-10-10
  • Vue使用watch监听一个对象中的属性的实现方法

    Vue使用watch监听一个对象中的属性的实现方法

    这篇文章主要介绍了Vue使用watch监听一个对象中的属性的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • 结合mint-ui移动端下拉加载实践方法总结

    结合mint-ui移动端下拉加载实践方法总结

    下面小编就为大家带来一篇结合mint-ui移动端下拉加载实践方法总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11

最新评论