当前位置:网站首页>2021-05-20computed and watch applications and differences
2021-05-20computed and watch applications and differences
2022-06-24 09:08:00 【Secret Shura】
VUE And computed and watch Application and difference Catalog :
** watch and computed**
conputed effect :
Calculate the result and return , Triggered only when the calculated value changes
( namely : The results of the calculated properties are cached , Recalculate only if the dependent reactive property changes , Has dependent properties .
( If you are in the name Method to print the time new Data() Multiple prints are the same , because new Data() Not dependent data )
new Vue({
el: '#id',
template: `<div> <span>Name: {
{name}}<span> </div>`,
data: {
firstName: 'Leo',
lastName: 'Alan'
},
computed: {
name () {
return `${
this.firstName} + ${
this.lastName}`
}
}
})
watch effect :
Listen for a value , When the value being monitored changes , Perform the corresponding operation , For example, the following single listening value , And then change data Or do other operations
( And computed Is the difference between the ,watch It is more suitable for monitoring the change of a certain value and doing corresponding operations , For example, request background interface , and computed It is applicable to calculating the existing value and returning the result )
( If you are in the name Print under method new Data() Printing many times is different , because watch A muscle , Update every time )
new Vue({
el: '#id',
template: `<div> // ... </div>`,
data: {
firstName: 'Leo',
lastName: 'Alan',
obj1: {
a: 0
}
},
watch: {
// monitor firstName, When firstName This function is executed when changes occur
firstName () {
// Perform the required actions ...
// notes : Initialization does not execute , Only if the monitored value (firstName) Only when changes occur
},
// monitor lastName
lastName: {
handler (newName, oldName) {
// Perform the required actions ...
},
immediate: true // true: During initialization, the corresponding operation of the listening will be executed first
},
obj1: {
handler () {
// Perform the required actions ...
},
deep: true // The default value of this property is false.
// When the monitored value is an object , Only deep by true when , The value of the corresponding attribute (obj1.a) The listening event can only be triggered when a change occurs , But this is very performance consuming
},
// Listen for specific properties of the object , deep You don't need to set it to true 了
'obj1.a': {
handler () {
// Perform the required actions ...
}
}
}
})
Be careful : Not in computed and watch The modification in is dependent ( Or be monitored ) Value , This may lead to an infinite loop
边栏推荐
猜你喜欢

学习太极创客 — ESP8226 (十三)OTA

用VNC Viewer的方式远程连接无需显示屏的树莓派

Spark - the number of leftouterjoin results is inconsistent with that of the left table

嵌入式 | 硬件转软件的几条建议

浮点数表示法(总结自CS61C和CMU CSAPP)

2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3

On the routing tree of gin

Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems

I heard that you are still spending money to buy ppt templates from the Internet?

【LeetCode】387. First unique character in string
随机推荐
[noi Simulation Competition] geiguo and time chicken (structure)
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
[pytorch basic tutorial 30] code analysis of DSSM twin tower model
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
Leetcode -- wrong set
threejs辉光通道01(UnrealBloomPass && layers)
金仓KFS replicator安装(Oracle-KES)
Kaformer personal notes
MBA-day25 最值问题-应用题
【牛客】HJ1 字符串最后一个单词的长度
PM2 deploy nuxt3 JS project
十二、所有功能实现效果演示
1528. 重新排列字符串
From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises
One article explains in detail | those things about growth
Data midrange: detailed explanation of the technical stack of data acquisition and extraction
【PyTorch基础教程30】DSSM双塔模型代码解析
解决:jmeter5.5在win11下界面上的字特别小
Pytoch read data set (two modes: typical data set and user-defined data set)
On the routing tree of gin