当前位置:网站首页>2021-05-20computed和watch应用与区别
2021-05-20computed和watch应用与区别
2022-06-24 07:06:00 【隐密修罗】
VUE之computed和watch应用与区别 目录:
** watch 和 computed**
conputed 作用:
计算结果并返回,只有当被计算的值发生改变时才会触发
(即:计算属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算,具有依赖属性.
(如果你在name方法下打印时间 new Data() 多次打印是一样的,因为new 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 作用:
监听某一个值,当被监听的值发生变化时,执行对应的操作,如下面的单个单个的监听值,然后再改变data的值或者做其他操作
(与computed的区别是,watch更加适用于监听某一个值的变化并做对应的操作,比如请求后台接口等,而computed适用于计算已有的值并返回结果)
(如果你在name方法下打印 new Data() 多次打印是不一样的,因为watch一根筋,每次都进行更新执行 )
new Vue({
el: '#id',
template: `<div> // ... </div>`,
data: {
firstName: 'Leo',
lastName: 'Alan',
obj1: {
a: 0
}
},
watch: {
// 监听firstName,当firstName发生变化时就会执行该函数
firstName () {
// 执行需要的操作...
// 注:初始化不会执行,只有当被监听的值(firstName)发生变化时才会执行
},
// 监听lastName
lastName: {
handler (newName, oldName) {
// 执行需要的操作...
},
immediate: true // true: 初始化时就会先执行一遍该监听对应的操作
},
obj1: {
handler () {
// 执行需要的操作...
},
deep: true // 该属性默认值为false.
// 当被监听的值是对象,只有deep为true时,对应属性的值(obj1.a)发生变化时才能触发监听事件,但是这样非常消耗性能
},
// 监听对象具体的属性, deep就不需要设置为true了
'obj1.a': {
handler () {
// 执行需要的操作...
}
}
}
})
注意: 不要在computed和watch中修改被依赖(或者被监听)的值,这样可能会导致无限循环
边栏推荐
- ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
- Vscode install the remote -wsl plug-in to connect to the local WSL
- 【力扣10天SQL入门】Day3
- ZUCC_编译语言原理与编译_实验03 编译器入门
- Permission model DAC ACL RBAC ABAC
- DataX User Guide
- Take my brother to do the project. It's cold
- [acnoi2022] not a structure, more like a structure
- Battle history between redis and me under billion level traffic
- The reason why the qtimer timer does not work
猜你喜欢
pymysql 向MySQL 插入数据无故报错
ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
jwt(json web token)
Redis cluster data skew
liunx服务器 telnet 带用户名 端口登陆方法
MySQL 因字符集问题插入中文数据时提示代码 :1366
【关于运维和网工的差别,一文说透】
關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
随机推荐
QT writing security video monitoring system 36 onvif continuous movement
Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
Building a static website with eleventy
Jenkins自动化部署,连接不到所依赖的服务【已解决】
Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
New technology practice, encapsulating the permission application library step by step with the activity results API
There was an error checking the latest version of pip
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
xtrabackup做数据备份
Common misconceptions in Tencent conference API - signature error_ code 200003
Markdown to realize text link jump
JS merge multiple objects and remove duplicates
Rescue system -- the application of read-write separation
ZUCC_ Principles of compiling language and compilation_ Big job
uniapp 热更新后台管理
Glusterfs replacement failure brick
Fundamentals of 3D mathematics [17] inverse square theorem
Two methods of QT exporting PDF files
Vscode install the remote -wsl plug-in to connect to the local WSL