当前位置:网站首页>Composition API (in setup) watch usage details
Composition API (in setup) watch usage details
2022-07-24 02:34:00 【Qing Qing】
Catalog
Two 、 A property of the listening object
3、 ... and 、 Listen to multiple objects or object properties
Four 、 combined api What on earth is the monitor ?
Premise (currentOperaMsg It's a responsive object )
As described below watch Monitor is Based on combination api Of , It's all written in setup Function
One 、 Listen to an object
1. Listen directly to an object : The first parameter is written directly that will do , The second argument is an arrow function
1) for example : listening object currentOperaMsg,
// Monitor patient information changes ( Update patient data when switching patients )
watch(currentOperaMsg, (newval, oldval) => {
console.log("watch-operaMsg",newval,oldval)
for(let[key,item] of (Object.entries(simpleInfo))){
simpleInfo[key].value=currentOperaMsg[key]
}
});2) result : A little confused , New objects newval And old object values oldval They are all changed , Nothing different .( I don't know why? )
3) And found that it seems to directly realize deep monitoring , Because I only changed patientname, As a result, this watch Function has print content .

Two 、 A property of the listening object
1. A property of the listening object : The first parameter needs to be returned by the arrow function
()=>currentOperaMsg.patientname
1) for example : listening object currentOperaMsg Properties in patientname
watch(
() => currentOperaMsg.patientname,
(newval, oldval) => {
console.log('watch-patientname', newval, oldval);
}
);2) result : normal , The values before and after the change are expected .
![]()
3、 ... and 、 Listen to multiple objects or object properties
1. Listen for multiple values : The first parameter is an array , Writing and 1、2 The statement is consistent with ;
The second parameter is also an arrow function , But the first and second parameters in the arrow function become Array . The first parameter in the arrow function stores the changed value , The second parameter stores the value before the change
1) for example : listening object currentOperaMsg and currentOperaMsg Properties in patientname
watch([currentOperaMsg,()=>currentOperaMsg.patientname],([newOperaMsg,newPatientname],[oldOperaMsg,oldPatientname])=>{
console.log(newOperaMsg,newPatientname,oldOperaMsg,oldPatientname)
})2) result : Such as 1.2 Described , Still consistent .

Four 、 combined api What on earth is the monitor ?
1. The listener must be a responsive object or a property of a responsive object ( It must be with ref and reactive Defined objects )
Changes in ordinary objects cannot be made in setup Monitored by . for example , The code below , There will be no printed content .
const testObj={
name:"lyl"
}
testObj.name=" I don't know how to fight "
watch(()=>testObj,(newval,oldval)=>{
console.log("watch-testObj",newval,oldval)
},
{
deep:true
})a. Sobbing , I really suddenly found this , But when you think about it, it seems that there is really no need to listen to such ordinary objects , Because on the page, anyone who needs to modify according to the change of variables will generally use Responsive variable .
b. More importantly, others Official documents : It directly indicates that in the combination API in ,watch It is used to listen for responsive changes .

5、 ... and 、 summary
1. Primary use , Feel unskilled , Make a note of . So that when you forget .
2. especially combined api in watch Only for arrow responsive object changes This must be kept in mind .
3. The values before and after the change when listening to an object mentioned above are printed It turned out to be exactly the same , It feels strange . And optional api The results are inconsistent . I don't know why yet , If any big guy knows , Welcome to comment and let us know .
/*
I hope it helps you !
If there is a mistake , Welcome to correct , Thank you very much !
*/
边栏推荐
- Mysql database, grouping function
- Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
- [untitled]
- Use of component El scrollbar
- [Luogu] p1318 ponding area
- 利用宝塔面板计划任务执行自动推送网址到百度收录
- Audio processing based on time-frequency diagram matlab
- Force open web page
- Codeworks 5 questions per day (average 1500) - day 23
- Essential skills for programmers -- breakpoint debugging (idea version)
猜你喜欢
![[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import](/img/4b/c24ac8a11d15285a49d7b3b9bde4e3.png)
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import
![[untitled]](/img/57/a3104833cb5fcc05916075f3bfdaf3.png)
[untitled]

認識傳輸層協議—TCP/UDP

One year after graduation, I gave up the internship opportunity and taught myself software testing at home. The internship of my classmates has just ended. I have become a 12K monthly salary testing e

The combination sum of C language power deduction question 39. Backtracking method and traversal method

记于2022.7.21

餐饮连锁门店重塑增长背后的数字化转型

BPG notes (III)

UIE: 信息抽取的大一统模型

Jina AI and datawhale jointly launched a learning project!
随机推荐
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part2):图谱数据准备与导入
Mysql数据库,排序与单行处理函数篇
[untitled]
What is naked SQL? What middleware or plug-in is good for express to operate MySQL?
ggplot2显示png
Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
Some consulting questions and answers raised by friends who lack programming foundation and want to change careers in ABAP development post
pbootcms模板调用标签序数从2开始或者自动数开始
Audio processing based on time-frequency diagram matlab
自定义log注解,请求抓取
Unity TimeLine使用教程
Qt开发串口通讯软件中的数据转换问题:读取时_QByteArray转string;发送时_数据格式;int转16进制格式string;string中截取字符;16进制数加法;string转ByteAr
Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
[diary of supplementary questions] [2022 Hangdian summer school 1] b-dragon Slayer
Research on XMPP service (I)
22 -- range and of binary search tree
The combination sum of C language power deduction question 39. Backtracking method and traversal method
【补题日记】[2022牛客暑期多校1]I-Chiitoitsu
C language curriculum - personal information management system (including student grades and consumption records)
网络协议详解 :UDP