当前位置:网站首页>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里面的结果不一致。尚不知道原因,如有大佬知道,欢迎评论告知。
/*
希望对你有帮助!
如有错误,欢迎指正,非常感谢!
*/
边栏推荐
- "Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- Rylstim Screen Recorder
- Hydrogen entrepreneurship competition | Liu Xiaoqi, chairman of Guohua Investment: take advantage of the integration of scenery, hydrogen storage and finance to host the entrepreneurship competition a
- Beansearcher receives array parameters and logical deletion
- Mysql数据库,分组函数篇
- Summary of the first change to open source middleware keycloak
- Live800: there is nothing trivial about customer service. Don't let service destroy the reputation of the enterprise
- Réalisation d'un diagramme de zone de ligne brisée - Diagramme Rose - Diagramme à barres
- ggplot2显示png
猜你喜欢

ggplot2显示png

Visual full link log tracking

Diablo king, analysis of low illumination image enhancement technology

JDBC tool class

BPG notes (III)

Implementation of POP3 client code

Ardunio - ULN2003 drive board and DC motor fan - control fan speed

Wechat applet setting background image does not display problem solution

氢能创业大赛 | 国华投资董事长刘小奇:发挥风光氢储融一体化优势 高水平承办创业大赛

暗黑系王者,低照度图像增强技术解析
随机推荐
记于2022.7.21
canvas-绘图(鼠标按下 绘制 抬起 结束)
Dynamically set the navigationbartitletext of the applet
输入cnpm -v出现cnpm : 无法加载文件 C:\Users\19457\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
Qml- use listview to build a three-level treeview architecture
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
Go basic notes_ 5_ Array slice
Jina AI 联合Datawhale,发起学习项目!
What is naked SQL? What middleware or plug-in is good for express to operate MySQL?
Vscade connects to the server. The password is correct, but it has been unable to connect
Go基础笔记_5_数组切片
Codeworks 5 questions per day (average 1500) - day 23
MySQL---four JDBC
[diary of supplementary questions] [2022 Hangdian summer school 1] c-backpack
LeetCode 70爬楼梯、199二叉树的右视图、232用栈实现队列、143重排链表
[diary of supplementary questions] [2022 Niuke summer school 1] d-mocha and railgun
相关性(correlation)
Backward quantum cryptography migration! NIST announces 12 Partners
22 -- range and of binary search tree