使用Vue3的watch实现数据的实时更新(附详细代码)

 更新时间:2025年08月20日 10:10:54   作者:Derlii  
vue.js是一个轻量级的前端框架,你可以使用它来实现数据实时刷新,下面这篇文章主要介绍了使用Vue3的watch实现数据的实时更新的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

watch函数用于侦听某个值的变化,当该值发生改变后,触发对应的处理逻辑。

一、监听基础ref类型

1.监听单个ref数据

<template>
  <button class="style" @click="num++">增加watch</button>
</template>

<script setup>
import { watch, ref } from "vue";
const num = ref(1);
// newVal: 新值 | oldVal: 旧值
watch(num, (newVal, oldVal) => {
  console.log(`新值:${newVal} --- 旧值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
  margin: 20px 20px;
}
</style>

初始值为1 点击按钮后 侦听到

2.监听多个ref数据,以数组的形式侦听

<template>
  <h1 class="style">{{ one }} | {{ two }}</h1>
  <button class="style" @click="one++">增加one的值</button>
  <button class="style" @click="two++">增加two的值</button>
</template>

<script setup>
import { watch, ref } from "vue";
const one = ref(0);
const two = ref(10);
// newVal: 新值 | oldVal: 旧值
watch([one, two], ([newVal, oldVal]) => {
  console.log(`新值:${newVal} --- 旧值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
  margin: 10px 20px;
}
</style>

one的初始值为1 two的初始值为10 点击one按钮后 侦听到 one的值+1

点击two按钮后 侦听到 two的值+1

3.监听一个ref对象

<template>
  <h1 class="style">{{ num.number }}</h1>
  <h1 class="style">{{ num.age }}</h1>
  <button class="style" @click="num.number++">增加number的值</button>
</template>

<script setup>
import { watch,ref } from "vue";
const num = ref({
  number: 1,
  age: 18,
});
// newVal: 新值 | oldVal: 旧值

// 这个侦听器无效,即控制台无输出
watch(num, (newVal, oldVal) => {
  console.log("侦听器1:", newVal, oldVal);
});

// getter函数形式,新旧值不一样
watch(
  () => ({ ...num.value }),
  (newVal, oldVal) => {
    console.log("侦听器2:", newVal, oldVal);
  }
);
</script>
<style lang="scss" scoped>
.style {
  margin: 10px 20px;
}
</style>

二、监听基础reactive类型

1.监听对象中的单个属性

<template>
  <h1 class="style">{{ num.value }}</h1>
  <button class="style" @click="num.value++">增加one的值</button>
</template>

<script setup>
import { watch, reactive } from "vue";
const num = reactive({
  value: 1,
});
// newVal: 新值 | oldVal: 旧值
watch(
  () => num.value,
  (newVal, oldVal) => {
    console.log(`新值:${newVal} --- 旧值:${oldVal}`);
  }
);
</script>
<style lang="scss" scoped>
.style {
  margin: 10px 20px;
}
</style>

初始值为1 点击按钮后 侦听到

2.多层嵌套的对象

<template>
  <h1 class="style">{{ num.number }}|{{ num.key.age }}</h1>
  <button class="style" @click="num.number++">增加number的值</button>
  <button class="style" @click="num.key.age++">增加age的值</button>
</template>

<script setup>
import { watch, reactive } from "vue";
const num = reactive({
  number: 1,
  name: "张三",
  key: {
    age: 18,
  },
});
// newVal: 新值 | oldVal: 旧值
watch(
  [() => num.number, () => num.key.age],
   (newVal, oldVal) => {
  console.log(`新值:${newVal} --- 旧值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
  margin: 10px 20px;
}
</style>

number的初始值为1 点击左侧按钮number+1 age的值不变

age的初始值为18 点击右侧按钮age+1 number值不变

3.同时监听ref基本类型数据和reactive对象中的属性

<template>
  <h1 class="style">{{ address }}|{{ num.number }}</h1>
  <button class="style" @click="address++">增加address的值</button>
  <button class="style" @click="num.number++">增加number的值</button>
</template>

<script setup>
import { watch, reactive, ref } from "vue";
const address = ref("88");
const num = reactive({
  number: 1,
  name: "张三",
  key: {
    age: 18,
  },
});
// newVal: 新值 | oldVal: 旧值
watch([address, () => num.number], (newVal, oldVal) => {
  console.log(`新值:${newVal} --- 新值:${newVal}`);
  console.log(`旧值:${oldVal}--- 旧值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
  margin: 10px 20px;
}
</style>

address的初始值为88 点击左侧按钮address+1 number的值不变

number的初始值为1 点击右侧按钮number+1 address的值不变 

总结

到此这篇关于使用Vue3的watch实现数据实时更新的文章就介绍到这了,更多相关Vue3 watch数据实时更新内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • React之input动态取值和赋值方式

    React之input动态取值和赋值方式

    这篇文章主要介绍了React之input动态取值和赋值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Vue3连接SSE并且返回结果用打字机效果呈现方法

    Vue3连接SSE并且返回结果用打字机效果呈现方法

    SSE的核心是一个持久的HTTP连接,服务端通过该连接不断发送文本格式的数据,下面这篇文章主要介绍了Vue3连接SSE并且返回结果用打字机效果呈现方法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2026-03-03
  • vue3源码分析reactivity实现原理

    vue3源码分析reactivity实现原理

    这篇文章主要为大家介绍了vue3源码分析reactivity实现原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • 详解Vue This$Store总结

    详解Vue This$Store总结

    这篇文章主要介绍了详解Vue This$Store总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Vue实现路由过渡动效的4种方法

    Vue实现路由过渡动效的4种方法

    Vue 路由过渡是对 Vue 程序一种快速简便的增加个性化效果的的方法,这篇文章主要介绍了Vue实现路由过渡动效的4种方法,感兴趣的可以了解一下
    2021-05-05
  • vue通过watch对input做字数限定的方法

    vue通过watch对input做字数限定的方法

    本篇文章主要介绍了vue通过watch对input做字数限定的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • vue配置路径别名方式(@/*)

    vue配置路径别名方式(@/*)

    文章介绍了如何配置路径别名,包括安装@types/node、编辑vite.config.ts或js文件、配置tsconfig.json以及重启软件等步骤
    2025-11-11
  • vue 利用路由守卫判断是否登录的方法

    vue 利用路由守卫判断是否登录的方法

    今天小编就为大家分享一篇vue 利用路由守卫判断是否登录的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • el-table树形表格表单验证(列表生成序号)

    el-table树形表格表单验证(列表生成序号)

    这篇文章主要介绍了el-table树形表格表单验证(列表生成序号),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Vue3+Vant实现简单的登录功能

    Vue3+Vant实现简单的登录功能

    这篇文章主要为大家详细介绍了Vue3如何结合Vant实现简单的登录功能,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下
    2024-04-04

最新评论