当前位置:网站首页>The use and difference of watch listening and computed calculation attributes
The use and difference of watch listening and computed calculation attributes
2022-06-29 10:39:00 【Upwind elegance】
watch and computed The difference between :
computed :
Supports caching , Only dependent data changes , To recalculate
Asynchronous is not supported , When computed Invalid if there is an asynchronous operation inside , Can't monitor data changes
3.computed Property value will default to cache , Computational properties are cached based on their responsive dependencies , That is based on data The data stated in is calculated
4. If an attribute is computed by another attribute , This property depends on other properties , It's a many to one or one to one , It's usually used computed
5. If computed Property property value is a function , So default will go get Method ; The return value of the function is the property value of the property ; stay computed Medium , All attributes have one get And a set Method , When data changes , call set Method .
watch:
- Caching is not supported , Data change , The corresponding operation will be triggered directly ;
2.watch Asynchronous Support ;
3. The listening function takes two parameters , The first parameter is the latest value ; The second parameter is the value before input ;
4. When an attribute changes , The corresponding operation needs to be performed ; One to many ;
5. If in data If there is no corresponding attribute in , Can not watch Of , This and computed Dissimilarity .
Use in actual projects :
watch:
Realization effect 
Code illustration :
watch:{
'tableData.scienceStaffNumPer':{ // Monitoring technicians input The number , Set radio buttons
//tableData.scienceStaffNumPer yes data The value defined in
handler(val){
if(!val){
this.tableData.scienceStaffNumFlag = null
return
}
var num =parseFloat(val)
if(num==0){
this.tableData.scienceStaffNumFlag = '0'
}
if (num>0&&num<5) {
this.tableData.scienceStaffNumFlag = '1'
}
if(num>=5&&num<10){
this.tableData.scienceStaffNumFlag = '2'
}
if(num>=10){
this.tableData.scienceStaffNumFlag = '3'
}
},
deep:true, // Listen now
immediate:true // Deep monitoring
},
}computed:
demand : The input box can only be entered 0-100 Number between , And keep two decimal places

Code illustration :
computed: {
techInputData: { // Scientific and technological personnel input The value entered in the input box
//techInputData It means that computed A data defined in , This field is bound to input In the input box
computed There are two ways , One is set, One is get get Return the calculated data
get() {
var value = this.tableData.scienceStaffNumPer // Assign the value of the input box to value
if (Number(value)!==parseFloat(value)) { // Determine whether the input is a number , If not , Returns an empty
return ''
}
if (value < 0) { // If the value entered is less than 0
return 0;
} else if (value >= 100) { // If the value entered is greater than or equal to 100
return 100;
} else {
return value.toString().match(/^\d+(?:\.\d{0,2})?/) // Only two decimal places can be entered
}
},
set(val) { // set Indicates the calculated data Set to A certain value
//val yes get Calculated data
this.tableData.scienceStaffNumPer = val; // Recalculate the processed value of the property , Assign to this input
},
},
}
边栏推荐
- IO流总结
- Recyclerview universal adapter package
- BUUCTF RE-easyre
- September 25, 2020 noncopyable of boost library for singleton mode
- 通过Win32API调用另一界面的按钮
- Downloading and installing VMware (basic idea + detailed process)
- MySQL InnoDB data length limit per row
- 全面理解MESI缓存一致性协议
- 基于STM32+RFID设计的宿舍检修管理系统
- 1- database understanding
猜你喜欢

The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month

Analysis of reentrantlock source code of AQS

Recyclerview sticky (suspended) head

有了这款工具,自动化识别验证码再也不是问题

MySQL InnoDB data length limit per row

BUUCTF RE-easyre

BUUCTF--内涵的软件

查看CSDN的博客排名

Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX

MySQL innodb每行数据长度的限制
随机推荐
拼图小游戏中学到的Graphics.h
Text of the basic component of the shutter
Hystrix fuse: Service fusing and service degradation
全面理解Volatile关键字
全面理解MESI缓存一致性协议
SQL Server 数据库的连接查询
Comprehensive understanding of synchronized
Add/modify/drop column of alter table operation in MySQL
Linux下Redis安装及集群搭建
IIS服务器相关错误
2019.11.20 training summary
Devaxpress double click to get cell data
C#MDI打开子窗体去掉自动生成的菜单栏
Installing and configuring wmware esxi 6.5.0 in VMware Workstation
Oracle重置序列发生器(非重建)
Slide the custom control to close the activity control
Report card of regional industrial Internet market, the second place of Baidu intelligent yunkaiwu
Analyze in detail the PBOT mining virus family behavior and the principle of exploited vulnerabilities, and provide detailed protection suggestions for the blue army
Learn spark computing framework in practice (01)
Excel日期及数字格式处理