如何使用 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>

 25c3d955de69476f8e5ffd6576dcc8b5.png

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

1550d4b63e0f4dd18816be0150957899.png

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>

a0dee0c301fd4691a1e08f30c0586746.png 

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

c776e4b3d44243d59772dfb2b9940010.png

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

8a32c6cd2940497591aed45b1a8cbc83.png

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>

297806f7ff174d13a73531f0e3049d36.png068ebef546274c0b86c662d16e9775b0.png

 二、监听基础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>

b881cb62de2347e9960a124ef862f060.png

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

b0aa1ee94c054ae98f9273628748fbe8.png

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>

f5fd99fafa5242caa86f1826863ae983.png

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

d8702f934bf94abdb448bf7d434bc07c.png

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

34ce93fafae843b9883349cdab13726e.png

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>

 0e9bfa009ed047a99000d70b893fe218.png

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

 b99ba1de64374ed3a41fad93538e2eae.png

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

ba157bd6326d4f6986222a177c597ec0.png

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值