当前位置:网站首页>11. Monitoring attribute - Watch
11. Monitoring attribute - Watch
2022-07-20 08:40:00 【Where the wind falls】
One 、 Monitoring properties watch
1. When the monitored attribute changes , Callback function automatically calls , Carry out relevant operations
2. Monitored properties must exist , To monitor
3. There are two ways to write surveillance
(1)new Vue Time passes in watch To configure
(2) adopt vm.$watch monitor
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title> Weather Cases _ Monitoring properties </title>
<!-- introduce Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--
Monitoring properties watch:
1. When the monitored attribute changes , Callback function automatically calls , Carry out relevant operations
2. Monitored properties must exist , To monitor !!
3. There are two ways to write surveillance :
(1).new Vue Time passes in watch To configure
(2). adopt vm.$watch monitor
-->
<!-- Prepare a container -->
<div id="root">
<h2> It's very... Today {
{info}}</h2>
<button @click="changeWeather"> Switch weather </button>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false // prevent vue Generate production prompts at startup .
const vm = new Vue({
el:'#root',
data:{
isHot:true,
},
computed:{
info(){
return this.isHot ? ' scorching hot ' : ' cool '
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
/* watch:{
isHot:{
immediate:true, // When initializing, let handler Call
//handler When to call ? When isHot When there is a change .
handler(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue)
}
}
} */
})
vm.$watch('isHot',{
immediate:true, // When initializing, let handler Call
//handler When to call ? When isHot When there is a change .
handler(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue)
}
})
</script>
</html>
Two 、 Deep surveillance
1.Vue Medium watch By default, changes in internal values of objects are not monitored ( First floor )
2. To configure deep:true You can monitor the changes of internal values of objects ( Multi-storey )
remarks :
vue It can monitor the changes of internal values of objects , but vue Provided watch By default, you can't
Use watch According to the specific structure of the data , Decide whether to use deep monitoring
Weather Cases :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title> Weather Cases _ Deep surveillance </title>
<!-- introduce Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--
Deep surveillance :
(1).Vue Medium watch By default, changes in internal values of objects are not monitored ( First floor ).
(2). To configure deep:true You can monitor changes in the internal values of objects ( Multi-storey ).
remarks :
(1).Vue It can monitor the change of the internal value of the object , but Vue Provided watch By default, you can't !
(2). Use watch According to the specific structure of the data , Decide whether to use deep monitoring .
-->
<!-- Prepare a container -->
<div id="root">
<h2> It's very... Today {
{info}}</h2>
<button @click="changeWeather"> Switch weather </button>
<hr/>
<h3>a The value of is :{
{numbers.a}}</h3>
<button @click="numbers.a++"> I'll let you know a+1</button>
<h3>b The value of is :{
{numbers.b}}</h3>
<button @click="numbers.b++"> I'll let you know b+1</button>
<button @click="numbers = {a:666,b:888}"> Completely replace numbers</button>
{
{numbers.c.d.e}}
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false // prevent vue Generate production prompts at startup .
const vm = new Vue({
el:'#root',
data:{
isHot:true,
numbers:{
a:1,
b:1,
c:{
d:{
e:100
}
}
}
},
computed:{
info(){
return this.isHot ? ' scorching hot ' : ' cool '
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
watch:{
isHot:{
// immediate:true, // When initializing, let handler Call
//handler When to call ? When isHot When there is a change .
handler(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue)
}
},
// Monitor the change of an attribute in the multi-level structure
/* 'numbers.a':{
handler(){
console.log('a Has been changed ')
}
} */
// Monitor the changes of all attributes in the multi-level structure
numbers:{
deep:true,
handler(){
console.log('numbers Changed the ')
}
}
}
})
</script>
</html>
3、 ... and 、 Monitoring attribute abbreviation
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title> Weather Cases _ Monitoring properties _ Abbreviation </title>
<!-- introduce Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- Prepare a container -->
<div id="root">
<h2> It's very... Today {
{info}}</h2>
<button @click="changeWeather"> Switch weather </button>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false // prevent vue Generate production prompts at startup .
const vm = new Vue({
el:'#root',
data:{
isHot:true,
},
computed:{
info(){
return this.isHot ? ' scorching hot ' : ' cool '
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
watch:{
// Normal writing
/* isHot:{
// immediate:true, // When initializing, let handler Call
// deep:true,// Deep surveillance
handler(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue)
}
}, */
// Abbreviation
/* isHot(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue,this)
} */
}
})
// Normal writing
/* vm.$watch('isHot',{
immediate:true, // When initializing, let handler Call
deep:true,// Deep surveillance
handler(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue)
}
}) */
// Abbreviation
/* vm.$watch('isHot',(newValue,oldValue)=>{
console.log('isHot It was modified ',newValue,oldValue,this)
}) */
</script>
</html>
边栏推荐
猜你喜欢
Easier to use C language entry-level cheese (2) select statements + loop statements, functions, arrays, operators (super detailed)
【学习笔记】Unreal(虚幻)4引擎入门(三)
Skillfully solve Young's matrix
[Day.2]约瑟夫环问题,如何用数组代替循环链表(详解)
Easily master | struct structure | knowledge points
A long detailed explanation of C language dynamic memory management
Heap sorting and heap related operations
基数排序(桶排序)
字符串逆序的几种写法
shell脚本编程
随机推荐
数组的 reduce方法
Skillfully solve Young's matrix
Part 59: main c:62:9: note: use option -std=c99 or -std=gnu99 to compile your code
C language program environment and preprocessing
Three piece chess game
【学习笔记】“STL演讲比赛流程管理系统”作业总结
进程与线程以及进线程间通信
How is data stored in memory?
69-简单聊天对话框-拓展-撤回功能
3. Data binding
14.过滤器的使用
更易上手的C语言入门级芝士 (1) 数据类型、变量和常量、字符串+转义字符+注释(超详细)
学习日记1
Learning diary 4- program structure and control statements
冒泡排序思想及实现
学习日记5-C语言函数的应用
区间覆盖问题
Pytorch target detection data processing (II) extracting difficult samples, low AP samples
Practice improving C Language-1
长篇详解C语言动态内存管理