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

2025-5-22Vue3快速上手

1.watch监视属性

(1)情况一:监视【ref】定义的【基本类型】数据

<template><div><h1>当前求和为:{{ sum }}</h1><button @click="changeSum">求和加一</button></div>
</template><script lang="ts">export default {name:'Person234'}
</script><script lang="ts" setup >
import {ref,reactive,toRef,toRefs, computed,watch} from 'vue'
let sum = ref(0);
const changeSum = function(){sum.value ++;
}
const stopWatch = watch(sum,(newVal,oldVal)=>{console.log(newVal,oldVal)if(sum.value >=10){stopWatch()
}})</script><style></style>

下面是stopWatch的值

(2)情况二:监视【ref】定义的【对象类型】数据

未开启深度监视时:点击修改姓名和点击修改年龄都不会触发watch

开启immidiate时,无论被监视的值有无变化,都会触发watch

以下是开启了deep和immidiate的情况

<template><div><h2>姓名:{{ person.name }}</h2><h2>年龄:{{ person.age }}</h2><button @click="changeName">修改姓名</button><button @click="changeAge">修改年龄</button><button @click="changePeron">修改人</button></div>
</template><script lang="ts">export default {name:'Person234'}
</script><script lang="ts" setup >
import {ref,reactive,toRef,toRefs, computed,watch} from 'vue'
const person = ref({name:'张三',age:18
})
const changeName = function(){person.value.name += '~'
}
const changeAge = function(){person.value.age ++
}
const changePeron = function(){person.value = {name:'李四',age:20}
}
//watch的第一个参数:被监视的数据
//第二个参数:回调函数
//第三个参数:配置项(如:deep,immidiate)
const stopWatch = watch(person,(newVal,oldVal)=>{console.log('person变化了',newVal,oldVal)
},{deep:true,immediate:true})
</script><style></style>

(3)情况三:监视【reactive】定义的【对象类型】数据,默认开启深度监视,该深度监视无法关闭

这是因为reactive本身创建的是一个深层响应式对象,其所有嵌套属性都是响应式的。

<template><div><h2>姓名:{{ person.name }}</h2><h2>年龄:{{ person.age }}</h2><button @click="changeName">修改姓名</button><button @click="changeAge">修改年龄</button><button @click="changePeron">修改人</button></div>
</template><script lang="ts">export default {name:'Person234'}
</script><script lang="ts" setup >
import {ref,reactive,toRef,toRefs, computed,watch} from 'vue'
const person = reactive({name:'张三',age:18
})
const changeName = function(){person.name += '~'
}
const changeAge = function(){person.age ++
}
const changePeron = function(){Object.assign(person,{name:'李四',age:80})
}
//watch的第一个参数:被监视的数据
//第二个参数:回调函数
//第三个参数:配置项(如:deep,immidiate)
const stopWatch = watch(person,(newVal,oldVal)=>{console.log('person变化了',newVal,oldVal)
})
</script><style></style>

(4)情况四:监视ref或reactive定义的【对象类型】数据的某个属性

1)若监视的属性是基本数据类型,需要写成函数形式(getter函数)

//监听基本数据类型
watch(()=>person.name,(newVal,oldVal)=>{console.log('person.name修改了',newVal,oldVal)
})

2)若监视的属性是对象类型,可直接写,也可写成函数形式,最好写成函数形式

直接写:会发现只能监听到C1或C2的修改。但是无法监听car整体的修改

//监听对象数据类型,直接写
watch(person.car,(newVal,oldVal)=>{console.log('person.car修改了',newVal,oldVal)
})

写成函数形式:只能监听car整体的修改,无法监听C1或C2的修改

//监听对象数据类型,函数形式
watch(()=>person.car,(newVal,oldVal)=>{console.log('person.car修改了',newVal,oldVal)
})

写成函数形式加上开启深度监视,就可以监听到car整体的修改和C1、C2的修改

//监听对象数据类型,函数形式
watch(()=>person.car,(newVal,oldVal)=>{console.log('person.car修改了',newVal,oldVal)
},{deep:true})
<template><div><h2>姓名:{{ person.name }}</h2><h2>年龄:{{ person.age }}</h2><h2>车1:{{ person.car.C1 }}</h2><h2>车2:{{ person.car.C2 }}</h2><button @click="changeName">修改姓名</button><button @click="changeAge">修改年龄</button><button @click="changeCar1">修改第一辆车</button><button @click="changeCar2">修改第二辆车</button><button @click="changeCar">修改整辆车</button></div>
</template><script lang="ts">export default {name:'Person234'}
</script><script lang="ts" setup >
import {ref,reactive,toRef,toRefs, computed,watch} from 'vue'
const person = reactive({name:'张三',age:18,car:{C1:'奔驰',C2:'宝马'}
})
const changeName = ()=>{person.name +='~'
}
const changeAge = ()=>{person.age +=1
}
const changeCar =()=>{person.car = {C1:'奥迪',C2:'大众'}}
const changeCar1 =()=>{person.car.C1 = '雅迪'
}
const changeCar2 =()=>{person.car.C2 = '爱码'
}
//监听基本数据类型
watch(()=>person.name,(newVal,oldVal)=>{console.log('person.name修改了',newVal,oldVal)
})
//监听对象数据类型,直接写
// watch(person.car,(newVal,oldVal)=>{
//   console.log('person.car修改了',newVal,oldVal)
// })
//监听对象数据类型,函数形式
watch(()=>person.car,(newVal,oldVal)=>{console.log('person.car修改了',newVal,oldVal)
},{deep:true})</script><style></style>

(5)情况五:监视以上几种情况

watch([()=>person.name,()=>person.car],(newVal,oldVal)=>{console.log('数组变化了',newVal,oldVal)
},{deep:true})

相关文章:

  • Linux--vim
  • Apache OFBiz 17.12.01 的远程命令执行漏洞 -Java 反序列化 + XML-RPC 请求机制
  • 深度学习面试八股简略速览
  • 互联网大厂Java求职面试:AI应用集成中的RAG系统优化与向量数据库性能调优实战
  • GDB调试工具详解
  • 异步编程与axios技术
  • [Excel VBA]如何製作買三送一優惠條件的POS結帳介面?
  • [特殊字符] UI-Trans:字节跳动发布的多模态 UI 转换大模型工具,重塑界面智能化未来
  • 基于云的内容中台核心优势是什么?
  • [Linux]如何配置mailutils郵件服務?
  • 云原生安全基石:Linux进程隔离技术详解
  • 基于Python的分布式网络爬虫系统设计与实现
  • 在 UVM验证环境中,统计 AXI协议的Outstanding Transactions
  • TDengine 对接微软 SSRS 报表系统
  • 《分布式年夜》解析
  • 容器与编排入门 - SRE 须知的 Docker 与 Kubernetes 基础
  • 力扣 74.搜索二维矩阵
  • ETL工具:Kettle,DataX,Flume,(Kafka)对比辨析
  • 【Linux】深刻理解OS管理
  • 电路图识图基础知识-回路编号及代号(四)
  • 广东人才网官方网站招聘信息/可以免费打开网站的软件
  • 西安北郊网站建设/女生学网络营销这个专业好吗
  • 动物自己做的网站/网站底部友情链接代码
  • 福州网站建设 大公司/推广方案怎么写
  • wordpress企业站教程/百度推广账户登录
  • 哪些网站可以接单做/深圳市推广网站的公司