当前位置: 首页 > news >正文

vue3+ts watch 整理

watch() 一共可以接受三个参数,侦听数据源、回调函数和配置选项

作用:监视数据的变化(和Vue2中的watch作用一致)

特点:Vue3中的watch只能监视以下四种数据:

ref定义的数据。
reactive定义的数据。
函数返回一个值(getter函数)。
一个包含上述内容的数组
ref 定义的数据

<template><div>str===={{ str }}</div><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPerson">点击修改部分属性</el-button><el-button @click="editPersonAll">点击修改全部</el-button><el-button @click="editString">修改基本数据类型</el-button></div>
</template><script setup lang="ts">
import { reactive, watch, ref, toRefs, Ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
// let str: Ref<string> = ref("AAA"); // 泛型写法
let str = ref("AAA");
let personObj = ref<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPerson = () => {personObj.value.name = "bbb";
};
const editPersonAll = () => {personObj.value.car = {price: 222,color: "blue",};
};const editString = () => {str.value = "Bbb";
};
// 监听基本数据类型变化
watch(() => str.value,(newValue, oldValue) => {console.log("监听基本数据类型变化newValue");console.log(newValue);console.log(oldValue);}
);
// 监听对象类型某个值的变化
watch(() => personObj.value.name,(newValue, oldValue) => {console.log("personObj.value.name");console.log("newValue==" + newValue);console.log("oldValue==" + oldValue);}
);/* 监视,情况一:监视【ref】定义的【对象类型】数据,监视的是对象的地址值,若想监视对象内部属性的变化,需要手动开启深度监视watch的第一个参数是:被监视的数据watch的第二个参数是:监视的回调watch的第三个参数是:配置对象(deep、immediate等等.....) */
watch(personObj,(newValue, oldValue) => {console.log("修改整个车");console.log("newValue==");let { car, name, age } = toRefs(newValue);console.log(car, name.value, age.value);console.log("oldValue==");// let {car,name,age} = toRefs(oldValue)// console.log(car,name,age);},{deep: true,}
);
</script><style scoped></style>

reactive 定义的数据
ref 写法

<template><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPerson">点击修改部分属性</el-button><el-button @click="editPersonAll">点击修改全部</el-button></div>
</template>
<script setup lang="ts">
import { reactive, watch, ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
let personObj = reactive<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPerson = () => {personObj.name = "bbb";
};
const editPersonAll = () => {personObj.car = {price: 222,color: "blue",};
};
//监听基本数据类型的写法
watch(() => personObj.name,(newValue, oldValue) => {console.log("newValue==" + newValue);console.log("oldValue==" + oldValue);}
);
监听对象类型的写法 (推荐使用这种方法)
// watch(
//   () => personObj.car,
//   (newValue, oldValue) => {
//     console.log("修改整个车");
//     console.log("newValue==" + newValue);
//     console.log("oldValue==" + oldValue);
//   }
// );
监听对象类型的写法
watch(personObj.car, (newValue, oldValue) => {console.log("修改整个车");console.log("newValue==" + newValue);console.log("oldValue==" + oldValue);
});
</script>

监听多个值变化
ref 写法

<template><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPersonName">点击修改name</el-button><el-button @click="editPersonCarColor">点击修改car-color</el-button></div>
</template><script setup lang="ts">
import { tr } from "element-plus/es/locales.mjs";
import { reactive, watch, ref, toRefs, Ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
let personObj = ref<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPersonName = () => {personObj.value.name = "bbb";
};
const editPersonCarColor = () => {personObj.value.car.color = "bule";
};// 监听对象类型某个值的变化
// 传入的是数组, 获取到的newValue 也是数组,一一对应的关系
watch([() => personObj.value.name, personObj.value.car],(newValue, oldValue) => {console.log("personObj.value--watch");console.log(newValue);console.log(oldValue);},{deep: true,}
);
</script><style scoped></style>

reactive 写法

<template><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPersonName">点击修改name</el-button><el-button @click="editPersonCarColor">点击修改car-color</el-button></div>
</template><script setup lang="ts">
import { tr } from "element-plus/es/locales.mjs";
import { reactive, watch, ref, toRefs, Ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
let personObj = reactive<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPersonName = () => {personObj.name = "bbb";
};
const editPersonCarColor = () => {personObj.car.color = "bule";
};// 监听对象类型某个值的变化
// 传入的是数组, 获取到的newValue 也是数组,一一对应的关系
watch([() => personObj.name, personObj.car],(newValue, oldValue) => {console.log("personObj.value--watch");console.log(newValue);console.log(oldValue);},{deep: true,}
);
</script><style scoped></style>

相关文章:

vue3+ts watch 整理

watch() 一共可以接受三个参数&#xff0c;侦听数据源、回调函数和配置选项 作用&#xff1a;监视数据的变化&#xff08;和Vue2中的watch作用一致&#xff09; 特点&#xff1a;Vue3中的watch只能监视以下四种数据&#xff1a; ref定义的数据。 reactive定义的数据。 函数返…...

【Elasticsearch入门到落地】6、索引库的操作

接上篇《5、安装IK分词器》 上一篇我们进行了IK分词器的安装与测试&#xff0c;本篇我们来学习ElasticSearch的索引库的操作&#xff0c;学习mapping映射属性以及CRUD操作。 一、前情回顾 我们在前几篇学习了ElasticSearch的基本概念&#xff0c;并动手搭建了ElasticSearch环…...

Java TCP可靠传输(1)

TCP 可靠传输 一. 确认应答 由发送方填充&#xff0c;再由接收方在序号的基础上1&#xff0c;填充到确认序号中&#xff0c;来表示已经接收到前面发送的&#xff0c;表明下一个从哪个位置发送。 二. 超时重传 数据在网络上传输时会经过很多网络设备&#xff0c;如果其中一个…...

ipad和macbook同步zotero文献附件失败的解决办法

背景&#xff1a;我所有的文献及其附件pdf都是在台式机&#xff08;windows系统&#xff09;&#xff0c;想要把这些文献同步到云上&#xff0c;然后再从云上同步到平板和其他笔记本电脑比如macbook。文献同步虽已成功&#xff0c;但文献附件都无法打开。 平板报错如下&#xf…...

linux-ubuntu学习笔记碎记

~指/home/user_name这个目录 查看软件安装目录&#xff1a;whereis vim 查看当前路径&#xff1a;pwd 终端中键入ctrls会挂起终端&#xff0c;即终端不响应键鼠&#xff1b;ctrlq可以恢复。 和虚拟机开启共享文件夹互传文件 点击桌面&#xff0c;按ctrlaltt&#xff0c;开…...

RV1126+FFMPEG推流项目(11)编码音视频数据 + FFMPEG时间戳处理

本节介绍本章节主要讲解的是push_server_thread线程的具体处理流程&#xff0c; push_server_thread这个线程的主要功能是通过时间戳比较&#xff0c;来处理音频、视频的数据并最终推流到SRT、RTMP、UDP、RTSP服务器 push_server_thread&#xff1a;流程如下 上图&#xff0c;…...

人工智能的出现,给生命科学领域的研究带来全新的视角|行业前沿·25-01-22

小罗碎碎念 今天和大家分享一份白皮书&#xff0c;系统总结并陈述人工智能在生命科学领域的应用。 人工智能在生命科学领域的应用&#xff0c;具体包括——单细胞转录组、疾病诊疗、医疗文本处理、RNA结构预测等多个方面&#xff0c;通过这份报告&#xff0c;我们可以详细了解相…...

python注释格式总结

三个双引号的用于文件&#xff0c;类&#xff0c;函数注释。 没有统一的规定&#xff0c;以下是比较清晰的写法。 文件注释&#xff08;文件顶部&#xff09;&#xff1a;文件用途空行作者信息&#xff08;每行一个键:值&#xff09; 类注释&#xff08;类名下行&#xff09…...

Django实现数据库的表间三种关系

Django实现数据库的表间三种关系 1. 一对多&#xff08;One-to-Many&#xff09;关系示例&#xff1a;关系说明&#xff1a;查询示例&#xff1a; 2. 一对一&#xff08;One-to-One&#xff09;关系示例&#xff1a;关系说明&#xff1a;查询示例&#xff1a; 3. 多对多&#x…...

C++蓝桥真题讲解

本篇文章和大家一起来试试一些简单的蓝桥真题 注意&#xff1a;本篇文章将全程使用devc和蓝桥官网&#xff0c;如果有小伙伴找不到devc安装包的可以本篇文章中下载。 赛前必知点 1.正式比赛时&#xff0c;先从蓝桥官网下载题目文档&#xff0c;然后用devc进行编译&#xff0c…...

【21】Word:德国旅游业务❗

目录 题目 NO1.2.3 NO4 NO5.6 NO7 NO8.9.10.11 题目 NO1.2.3 F12&#xff1a;另存为布局→页面设置→页边距&#xff1a;上下左右选中“德国主要城市”→开始→字体对话框→字体/字号→文本效果&#xff1a;段落对话框→对齐方式/字符间距/段落间距 NO4 布局→表对话框…...

如何分辨ddos攻击和cc攻击?

DDoS&#xff08;分布式拒绝服务&#xff09;攻击和 CC&#xff08;Challenge Collapsar&#xff09;攻击都属于网络攻击手段&#xff0c;主要通过消耗目标服务器资源使其无法正常提供服务&#xff0c;但它们在攻击原理、攻击特征等方面存在区别&#xff1a; 攻击原理 DDoS 攻…...

enum EPOLL_EVENTS详解

enum EPOLL_EVENTS 是 Linux 中 epoll 机制的核心定义之一&#xff0c;它定义了 epoll 支持的所有事件类型。每个事件类型对应一个唯一的位掩码&#xff08;bitmask&#xff09;&#xff0c;通过按位或&#xff08;|&#xff09;可以组合多个事件类型&#xff0c;通过按位与&am…...

阿里前端开发规范

文章目录 1. 为什么前端写代码要规范&#xff1f;一、代码规范的必要性二、 规范带来的好处 2. 资源一、推荐 1. 为什么前端写代码要规范&#xff1f; 一、代码规范的必要性 可维护性 统一的代码风格便于理解和修改减少代码维护成本降低项目交接难度 团队协作 提高团队开发效…...

从函数式编程到响应式编程:现代开发中的范式转变

引言 随着软件开发领域的不断进化&#xff0c;编程范式也在经历着一场又一场的变革。从面向过程到面向对象&#xff0c;再到近年来流行的函数式编程和响应式编程&#xff0c;开发者正逐步适应不同的编程思想来解决现代软件开发中的复杂问题。本文将带你了解函数式编程和响应式编…...

Django学习笔记(启动项目)-03

Django学习笔记(启动项目)-03 1、在urls文件中配置一个路由url 2、在views文件中创建视图函数 3、启动项目测试结果 # 输入项目启动命令 python manage.py runserver4、创建HTML模版和静态文件 1、在templates文件夹中创建一个html 2、创建url路由与视图函数 3、测试效果 4、…...

量变引起质变

量变引起质变&#xff0c;这个是最本质的规律&#xff0c;重复进行一件事情&#xff0c;这件事情就会越来越完善&#xff0c;越来越完美&#xff0c;哪怕是菜鸟&#xff0c;重复多了就是大佬。 我从说话结结巴巴&#xff0c;到说话流畅&#xff0c;只是用了15天直播写代码&…...

NewStar CTF week1 web wp

谢谢皮蛋 做这题之前需要先去学习一些数据库的知识 1 order by 2 1可以理解为输入的id&#xff0c;是一个占位符&#xff0c;按第二列排序用来测试列数&#xff0c;如果没有两列则会报错-1 union select 1,2 -1同样是占位符&#xff0c;union的作用是将注入语句合并到原始语句…...

李沐vscode配置+github管理+FFmpeg视频搬运+百度API添加翻译字幕

终端输入nvidia-smi查看cuda版本 我的是12.5&#xff0c;在网上没有找到12.5的torch&#xff0c;就安装12.1的。torch&#xff0c;torchvision&#xff0c;torchaudio版本以及python版本要对应 参考&#xff1a;https://blog.csdn.net/FengHanI/article/details/135116114 创…...

深度学习中Batch Normalization(BN)原理、作用浅析

最近做剪枝学习&#xff0c;其中一种是基于BN层的γ作为缩放因子进行剪枝的&#xff0c;那么我想搞懂BN的工作原理更好的理解网络、剪枝等&#xff0c;所以有了该文。 首先先说BN的作用在详细拆解&#xff0c;理解。以知乎一条高赞评论说明BN层到底在干什么。 Batch Norm 为什…...

多云管理“拦路虎”:深入解析网络互联、身份同步与成本可视化的技术复杂度​

一、引言&#xff1a;多云环境的技术复杂性本质​​ 企业采用多云策略已从技术选型升维至生存刚需。当业务系统分散部署在多个云平台时&#xff0c;​​基础设施的技术债呈现指数级积累​​。网络连接、身份认证、成本管理这三大核心挑战相互嵌套&#xff1a;跨云网络构建数据…...

Objective-C常用命名规范总结

【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名&#xff08;Class Name)2.协议名&#xff08;Protocol Name)3.方法名&#xff08;Method Name)4.属性名&#xff08;Property Name&#xff09;5.局部变量/实例变量&#xff08;Local / Instance Variables&…...

【第二十一章 SDIO接口(SDIO)】

第二十一章 SDIO接口 目录 第二十一章 SDIO接口(SDIO) 1 SDIO 主要功能 2 SDIO 总线拓扑 3 SDIO 功能描述 3.1 SDIO 适配器 3.2 SDIOAHB 接口 4 卡功能描述 4.1 卡识别模式 4.2 卡复位 4.3 操作电压范围确认 4.4 卡识别过程 4.5 写数据块 4.6 读数据块 4.7 数据流…...

Neo4j 集群管理:原理、技术与最佳实践深度解析

Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...

基于Docker Compose部署Java微服务项目

一. 创建根项目 根项目&#xff08;父项目&#xff09;主要用于依赖管理 一些需要注意的点&#xff1a; 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件&#xff0c;否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同&#xff0c;结合所安装的tensorflow的目录结构修改from语句即可。 原语句&#xff1a; from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后&#xff1a; from tensorflow.python.keras.lay…...

《C++ 模板》

目录 函数模板 类模板 非类型模板参数 模板特化 函数模板特化 类模板的特化 模板&#xff0c;就像一个模具&#xff0c;里面可以将不同类型的材料做成一个形状&#xff0c;其分为函数模板和类模板。 函数模板 函数模板可以简化函数重载的代码。格式&#xff1a;templa…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

&#x1f4cc; LRU 缓存机制详解与实现&#xff08;Java版&#xff09; 一、&#x1f4d6; 问题背景 在日常开发中&#xff0c;我们经常会使用 缓存&#xff08;Cache&#xff09; 来提升性能。但由于内存有限&#xff0c;缓存不可能无限增长&#xff0c;于是需要策略决定&am…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

数据结构第5章:树和二叉树完全指南(自整理详细图文笔记)

名人说&#xff1a;莫道桑榆晚&#xff0c;为霞尚满天。——刘禹锡&#xff08;刘梦得&#xff0c;诗豪&#xff09; 原创笔记&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 上一篇&#xff1a;《数据结构第4章 数组和广义表》…...