【Vue3】watch 监视多种类型数据

【Vue3】watch 监视多种类型数据

背景

随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创,大多是参考其他文章资料整理所得,感谢每位技术人的开源精神。

简介

本文介绍 Vue3 中如何使用 watch 函数监视多种类型的数据。

开发环境

分类 名称 版本
操作系统 Windows Windows 11
IDE Visual Studio Code 1.91.1

开发步骤及源码

【Vue3】watch 监视对象类型数据中的某个属性 基础上修改 Vue 根组件 App.vue 代码。

复制代码
<template>
  <div class="person">
    <h1>监视多种类型数据</h1>
    <h2>姓名:{{ person.name }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <h2>电影:{{ person.film.f1 }} | {{ person.film.f2 }}</h2>
    <button @click="growUp">长大</button>
    <button @click="changeFilm">修改全部电影</button>
    <button @click="changeFilm1">修改第一部电影</button>
    <button @click="changeFilm2">修改第二部电影</button>
  </div>
</template>

<script setup lang="ts" name="App">
import { reactive, watch } from 'vue'

const person = reactive({
  name: 'Harry Potter',
  age: 10,
  film: {
    f1: '哈利·波特与魔法石',
    f2: '哈利·波特与密室',
  }
})

function growUp() {
  person.age += 1
}

function changeFilm() {
  person.film = {
    f1: '哈利·波特与阿兹卡班的囚徒',
    f2: '哈利·波特与火焰杯',
  }
}

function changeFilm1() {
  person.film.f1 = '哈利·波特与凤凰社'
}

function changeFilm2() {
  person.film.f2 = '哈利·波特与混血王子'
}

watch([() => person.age, () => person.film], (newValue, oldValue) => {
  console.log('Data changed from', oldValue, 'to', newValue)
}, {
  deep: true
})
</script>

<style scoped>
button {
  margin-right: 10px;
}
</style>

同时监视多种类型数据,只需将被监视数据包装成一个数组,将此数组作为 watch 函数的第一个参数传入。此时需要注意 watch 函数第二个参数中 newValueoldValue 的值。

从日志中可以看出,newValueoldValueProxy 对象,其中也包含一个数组,对应被监视数据的变化,可以如以下方式进行调用。

复制代码
<template>
  <div class="person">
    <h1>监视多种类型数据</h1>
    <h2>姓名:{{ person.name }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <h2>电影:{{ person.film.f1 }} | {{ person.film.f2 }}</h2>
    <button @click="growUp">长大</button>
    <button @click="changeFilm">修改全部电影</button>
    <button @click="changeFilm1">修改第一部电影</button>
    <button @click="changeFilm2">修改第二部电影</button>
  </div>
</template>

<script setup lang="ts" name="App">
import { reactive, watch } from 'vue'

const person = reactive({
  name: 'Harry Potter',
  age: 10,
  film: {
    f1: '哈利·波特与魔法石',
    f2: '哈利·波特与密室',
  }
})

function growUp() {
  person.age += 1
}

function changeFilm() {
  person.film = {
    f1: '哈利·波特与阿兹卡班的囚徒',
    f2: '哈利·波特与火焰杯',
  }
}

function changeFilm1() {
  person.film.f1 = '哈利·波特与凤凰社'
}

function changeFilm2() {
  person.film.f2 = '哈利·波特与混血王子'
}

watch([() => person.age, () => person.film], (newValue, oldValue) => {
  console.log('Data changed from', oldValue, 'to', newValue)
  console.log('%s 年龄从 %d 长大到 %d', person.name, oldValue[0], newValue[0])
  console.log(person.name, '出演电影', JSON.stringify(newValue[1]))
}, {
  deep: true
})
</script>

<style scoped>
button {
  margin-right: 10px;
}
</style>
相关推荐
是梦终空17 小时前
JAVA毕业设计259—基于Java+Springboot+vue3工单管理系统的设计与实现(源代码+数据库+开题报告)
java·spring boot·vue·毕业设计·课程设计·工单管理系统·源代码
@AfeiyuO19 小时前
封装表格操作列按钮
vue
千寻技术帮1 天前
10382_基于Springboot的高校排课管理系统
mysql·vue·毕设·spingboot·高校排课
IT教程资源C1 天前
(N_115)基于springboot,vue教务管理系统
mysql·vue·前后端分离·springboot教务系统
沧澜sincerely1 天前
WebSocket 实时聊天功能
网络·websocket·vue·springboot
苏打水com1 天前
第二十篇:Day58-60 前端性能优化进阶——从“能用”到“好用”(对标职场“体验优化”需求)
前端·css·vue·html·js
Jeking2171 天前
进阶流程图绘制工具 Unione Flow Editor-- 直击行业痛点:高扩展性解决方案解析
vue·流程图·workflow·unione flow·flow editor·unione cloud
旧梦星轨1 天前
掌握 Vite 环境配置:从 .env 文件到运行模式的完整实践
前端·前端框架·node.js·vue·react
sg_knight1 天前
模块热替换 (HMR):前端开发的“魔法”与提速秘籍
前端·javascript·vue·浏览器·web·模块化·hmr
Jeking2171 天前
进阶流程图绘制工具 Unione Flow Editor-- 巧用Event事件机制,破解复杂业务交互难题
流程图·vue3·workflow·unione flow·flow editor·unione cloud