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

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。首先,需要安装 echarts 和 vue-echarts 两个依赖。 然后,可以在 Vue3 中使用 `setup` 语法糖创建一个组件,并在其中使用 `ref` 来创建一个响应式数据。例如: ```javascript import { defineComponent, ref } from 'vue' import * as echarts from 'echarts' import { use } from 'echarts/core' import { CanvasRenderer } from 'echarts/renderers' use([CanvasRenderer]) export default defineComponent({ setup() { const chartData = ref({ xData: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], yData: [820, 932, 901, 934, 1290, 1330, 1320] }) const chartRef = ref(null) const updateChartData = () => { // 模拟数据实时更新 setInterval(() => { chartData.value.yData = chartData.value.yData.map(item => item + Math.floor(Math.random() * 200)) }, 2000) } return { chartData, chartRef, updateChartData } }, mounted() { this.updateChartData() }, render() { const options = { xAxis: { type: 'category', data: this.chartData.xData }, yAxis: { type: 'value' }, series: [{ data: this.chartData.yData, type: 'line' }] } return ( <div ref="chartRef" style="width: 100%; height: 500px;"></div> ) }, watch: { chartData: { handler(val) { this.$refs.chartRef.chart.setOption({ series: [{ data: val.yData }] }) }, deep: true } } }) ``` 在 `setup` 函数中,我们使用 `ref` 创建了一个名为 `chartData` 的响应式数据,它包含了 x 轴和 y 轴的数据。我们还创建了一个 `chartRef` 的引用,用于在 `mounted` 生命周期钩子中初始化图表。同时,我们还定义了一个 `updateChartData` 函数,用于模拟数据实时更新的情况。 在 `render` 函数中,我们通过 `this.chartData` 和 `this.chartRef` 获取到了我们需要的数据和 DOM 引用。我们使用 options 对象来配置图表选项,并将其渲染到 DOM 中。 在 `watch` 函数中,我们监听了 `chartData` 的变化,并在数据变化时更新图表。我们通过 `$refs` 获取到了 `chartRef` 的引用,并使用 `setOption` 方法更新了图表的数据。 以上就是使用 Vue3 `setup` 语法糖实现 echarts 折线图数据实时更新的方法。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值