当前位置:网站首页>11_ Weather case - monitoring properties
11_ Weather case - monitoring properties
2022-07-26 23:52:00 【ID does not exist.】
Listening properties
Monitoring properties watch:
1. When the monitored attribute changes , Callback function automatically calls , Carry out relevant operations
2. The monitored property must exist to be monitored
3. There are two ways to write surveillance :
(1)new Vue Time passes in watch To configure
(2) adopt vm.$watch monitor
<body>
<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
}
},
// new Vue Time passes in watch To configure
watch:{
isHot:{
immediate:true,
handler(oldValue,newValue){
console.log(oldValue,newValue);
}
}
}
</script>
<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
}
},
// adopt vm.$watch monitor
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>
4. 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 )
5. 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
<body>
<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>
{
{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: {
//handler When to call ? When isHot When there is a change .
handler(newValue, oldValue) {
console.log('isHot It was modified ', newValue, oldValue)
}
},
// Monitor the changes of all attributes in the multi-level structure
numbers: {
deep: true,
handler() {
console.log('numbers Changed the ')
}
}
}
})
</script>
<body>
<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: {
// Abbreviation
/* isHot(newValue,oldValue){
console.log('isHot It was modified ',newValue,oldValue,this)
} */
}
})
// Abbreviation
/* vm.$watch('isHot',(newValue,oldValue)=>{
console.log('isHot It was modified ',newValue,oldValue,this)
}) */
</script>
6.computed And watch The difference between :
(1)computed What can be done ,watch All can be done
(2)watch What can be done ,computed It doesn't have to be done , for example :watch You can do asynchronous operations
7. Two principles :
(1) All being Vue Management functions , It's best to write it as an ordinary function , such this Pointing is vm Or component instance object
(2) All are not Vue Management functions ( Timer callback function 、ajax Callback function, etc 、promise Callback function for ), It's best to write it as an arrow function , such this The point is vm Or component instance object
<body>
<div id="root">
surname :<input type="text" v-model="firstName"> <br /><br />
name :<input type="text" v-model="lastName"> <br /><br />
full name :<span>{
{fullName}}</span> <br /><br />
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false // prevent vue Generate production prompts at startup .
const vm = new Vue({
el: '#root',
data: {
firstName: ' Zhang ',
lastName: ' 3、 ... and ',
fullName: ' Zhang - 3、 ... and '
},
watch: {
firstName(val) {
setTimeout(() => {
console.log(this)
this.fullName = val + '-' + this.lastName
}, 1000);
},
lastName(val) {
this.fullName = this.firstName + '-' + val
}
}
})
</script>
边栏推荐
- 文件上传到服务器
- Last week's hot review (7.11-7.17)
- 第二部分—C语言提高篇_9. 链表
- The nature and proof of the center of gravity of [mathematics] tree
- 第二部分—C语言提高篇_8. 文件操作
- 银河证券网上开户佣金,网上客户经理开户安全吗
- Part II - C language improvement_ 9. Linked list
- Use Arthas to locate online problems
- 公有云安全性和合规性方面的考虑事项
- C language dynamic memory management
猜你喜欢

NFT display guide: how to display your NFT collection

At 12:00 on July 17, 2022, the departure of love life on June 28 was basically completed, and it needs to rebound

Practice of intelligent code reconstruction of Zhongyuan bank

1. Configuration environment and project creation

C language array

Thousands of tiles' tilt model browsing speeds up, saying goodbye to the embarrassment of jumping out one by one

Three effective strategies for the transformation of data supply chain to be coordinated and successful

Pytorch学习记录(二):张量

第二部分—C语言提高篇_9. 链表

An online accident, I suddenly realized the essence of asynchrony
随机推荐
DHCP, VLAN, NAT, large comprehensive experiment
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
第二部分—C语言提高篇_8. 文件操作
MySQL syntax uses detailed code version
告别宽表,用 DQL 成就新一代 BI
Which securities company has the lowest commission? Is online account opening safe
Everything you should know about wearable NFT!
[H5 bottom scrolling paging loading]
力扣155题,最小栈
企业数据治理面临的六大挑战!
【不积跬步无以至千里】统计日志指定时间段内的关键词
Tensorflow2.0 深度学习运行代码简单教程
Part II - C language improvement_ 10. Function pointer and callback function
分页插件--PageHelper
08 design of intelligent agricultural environmental monitoring system based on ZigBee
关于可穿戴式NFT你应该知道的一切!
【面试:并发篇27:多线程:犹豫模式】
Custom type
上千Tile的倾斜模型浏览提速,告别一块一块往外蹦的尴尬
Concept of functional interface & definition and use of functional interface