当前位置:网站首页>compostion-api(setup中) watch使用细节
compostion-api(setup中) watch使用细节
2022-07-24 02:32:00 【琹箐】
目录
前提(currentOperaMsg是一个响应式对象)
以下所述的watch监听是基于组合式api的,都是写在 setup函数中的
一、监听某一个对象
1.直接监听某个对象:第一个参数直接写即可,第二个参数是一个箭头函数
1)例如:监听对象currentOperaMsg,
// 监听患者信息变化(切换患者时更新患者数据)
watch(currentOperaMsg, (newval, oldval) => {
console.log("watch-operaMsg",newval,oldval)
for(let[key,item] of (Object.entries(simpleInfo))){
simpleInfo[key].value=currentOperaMsg[key]
}
});2)结果:有点迷惑,新的对象newval和旧的对象值oldval都是变更后的,没有任何不同。(我也不知道为什么)
3)而且发现貌似直接实现了深度监听,因为我只改变了patientname,结果这个watch函数有打印内容。

二、监听对象的一个属性
1.监听对象的某一个属性:第一个参数需要用箭头函数返回
()=>currentOperaMsg.patientname
1) 例如:监听对象currentOperaMsg中的属性 patientname
watch(
() => currentOperaMsg.patientname,
(newval, oldval) => {
console.log('watch-patientname', newval, oldval);
}
);2)结果:正常,更改前后的值是预期的。
![]()
三、监听多个对象或对象的属性
1.监听多个值:第一个参数是一个数组,写法和1、2所述一致;
第二个参数也是一个箭头函数,但是箭头函数里面的第一个参数和第二个参数都变成了 数组。箭头函数中第一个参数存储变化后的值,第二个参数存储变化前的值
1)例如:监听对象currentOperaMsg 和 currentOperaMsg中的属性 patientname
watch([currentOperaMsg,()=>currentOperaMsg.patientname],([newOperaMsg,newPatientname],[oldOperaMsg,oldPatientname])=>{
console.log(newOperaMsg,newPatientname,oldOperaMsg,oldPatientname)
})2)结果:如1.2所述,还是一致的。

四、组合式api 监听的到底是什么?
1.监听的必须是响应式对象或者响应式对象的属性(必须是用ref和reactive定义的对象)
普通对象的变化是无法在setup中监听的。例如,下方代码,是不会有任何打印内容的。
const testObj={
name:"lyl"
}
testObj.name="不知打"
watch(()=>testObj,(newval,oldval)=>{
console.log("watch-testObj",newval,oldval)
},
{
deep:true
})a.呜呜,这个我是真的突然发现的,但是仔细想想好像确实没有必要监听这种普通对象,因为页面上但凡需要据变量变化修改的一般都会使用 响应式变量。
b.更重要的是人家 官方文档:直接指明了在组合式API中,watch是用来监听响应式变化的。

五、总结
1.初用,感觉不熟练,记录一下。以便忘记时查阅。
2.特别是 组合式api里 watch只用于箭头响应式对象变化 这点一定要牢记。
3.前面提到的监听一个对象时变化前和变化后的值打印出来 竟然是一模一样的,感觉有些奇怪。和选项式api里面的结果不一致。尚不知道原因,如有大佬知道,欢迎评论告知。
/*
希望对你有帮助!
如有错误,欢迎指正,非常感谢!
*/
边栏推荐
- 【补题日记】[2022牛客暑期多校1]D-Mocha and Railgun
- Visual full link log tracking
- Async await details & Promise
- Redis 6.0 source code learning simple dynamic string
- Ardunio - ULN2003 drive board and DC motor fan - control fan speed
- 【补题日记】[2022牛客暑期多校1]I-Chiitoitsu
- 【补题日记】[2022杭电暑期多校1]C-Backpack
- Rylstim Screen Recorder
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- Essential skills for programmers -- breakpoint debugging (idea version)
猜你喜欢

利用宝塔面板计划任务执行自动推送网址到百度收录

Give me five minutes, give you a "cloud"

Live800:客户服务无小事,别让服务击溃企业口碑

This article shows you how to use SQL to process weekly report data

Backward quantum cryptography migration! NIST announces 12 Partners

Qml- use listview to build a three-level treeview architecture
![[untitled]](/img/61/91a8a67d069193a9f3000a43ccdab9.png)
[untitled]

POP3客户端代码的实现

Doodle Icons - 一组免费商用的涂鸦风格图标库,可爱轻快又独特

Understand the transport layer protocol - tcp/udp
随机推荐
Summary of the first change to open source middleware keycloak
Use of component El scrollbar
JDBC tool class
Loadrunner12 installation, recording the first script and the proxy server did not respond to the solution
响应式pbootcms模板装修设计类网站
Chinese scientists have made new progress in high security quantum key distribution networks
[FPGA tutorial case 39] communication case 9 - interleaving deinterleaving data transmission based on FPGA
No coding is required, and the "asynchronous request reply" mode is automatically implemented
Installation, configuration and use of sentry
认识传输层协议—TCP/UDP
IBM: realize the quantum advantage of fault tolerance by 2030
【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I
Mysql数据库,分组函数篇
Responsive pbootcms template decoration design website
利用宝塔面板计划任务执行自动推送网址到百度收录
[diary of supplementary questions] [2022 Niuke summer school 1] d-mocha and railgun
NetApp FAS系列一个CIFS bug引起的控制器重启案例分享
关于 SAP Fiori 应用的离线使用
What is naked SQL? What middleware or plug-in is good for express to operate MySQL?
Brief introduction of tfw6524 perfectly replacing imported pt6524 chip