当前位置:网站首页>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里面的结果不一致。尚不知道原因,如有大佬知道,欢迎评论告知。
/*
希望对你有帮助!
如有错误,欢迎指正,非常感谢!
*/
边栏推荐
- Crop leaf disease identification system
- Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
- [untitled]
- Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
- Emmet syntax summary
- 微信小程序實現折線面積圖-玫瑰圖-立體柱狀圖
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- [untitled]
- Live800: there is nothing trivial about customer service. Don't let service destroy the reputation of the enterprise
- Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection
猜你喜欢

Reconnaître le Protocole de couche de transport - TCP / UDP

canvas-绘图(鼠标按下 绘制 抬起 结束)

网络协议详解 :UDP

Research on XMPP service (I)

Wechat applet setting background image does not display problem solution

Brief introduction of tfw6524 perfectly replacing imported pt6524 chip

暗黑系王者,低照度图像增强技术解析

2022.7.22 JS entry common data types and methods

Codeworks 5 questions per day (average 1500) - day 23
![[Luogu] p1318 ponding area](/img/94/ed0c27c0be3759f83c0290d4b7801b.jpg)
[Luogu] p1318 ponding area
随机推荐
[diary of supplementary questions] [2022 Niuke summer multi school 2] k-link with bracket sequence I
BPG notes (III)
云原生讲解【扩展篇】
[leetcode] sword finger offer 61. shunzi in playing cards
Emmet syntax summary
Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
2022-07-22: what is the output of the following go language code? A:1; B:1.5; C: Compilation error; D:1.49。 package main import “fmt“ func main() { var i
Leetcode exercise -- two questions about the nearest common ancestor of binary trees
2022.7.22 JS entry common data types and methods
营员招募|心怀世界的AI青年们,联合国需要你为可持续发展助力!
7 issues to consider before website construction
Seatunnel architecture
【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I
Qml- use listview to build a three-level treeview architecture
微信小程序實現折線面積圖-玫瑰圖-立體柱狀圖
Redraw the button and make your own circular LED indicator
The combination sum of C language power deduction question 39. Backtracking method and traversal method
22 -- range and of binary search tree
[diary of supplementary questions] [2022 Niuke summer school 1] c-grab the seat
WordPress website SEO complete tutorial