当前位置:网站首页>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
边栏推荐
- 【LeetCode】415. 字符串相加
- 阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
- How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
- JS to find and update the specified value in the object through the key
- Mba-day25 best value problem - application problem
- Redis implements a globally unique ID
- [MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
- Determination of monocular and binocular 3D coordinates
- [redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
- 开源之夏中选名单已公示,基础软件领域成为今年的热门申请
猜你喜欢

从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”

What is graph neural network? Figure what is the use of neural networks?

Target detection series fast r-cnn

【LeetCode】541. 反转字符串 II

linux(centos7.9)安装部署mysql-cluster 7.6

110. 平衡二叉树-递归法

深入了解 border

Lu Qi: I am most optimistic about these four major technology trends

【Pytorch基础教程31】YoutubeDNN模型解析

【E325: ATTENTION】vim编辑时报错
随机推荐
Liunx change the port number of vsftpd
Database migration from PostgreSQL to MySQL
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
小白学习MySQL - 增量统计SQL的需求
Linux MySQL installation
[e325: attention] VIM editing error
Data middle office: middle office practice and summary
Groovy通过withCredentials获取Jenkins凭据
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
双指针模拟
【牛客】HJ1 字符串最后一个单词的长度
听说你还在花钱从网上买 PPT 模板?
uniapp 开发多端项目如何配置环境变量以及区分环境打包
Spark - LeftOuterJoin 结果条数与左表条数不一致
1528. 重新排列字符串
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
Double pointer analog
110. balanced binary tree recursive method
MySQL | 存储《康师傅MySQL从入门到高级》笔记