当前位置:网站首页>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>
边栏推荐
- Part II - C language improvement_ 9. Linked list
- Use Arthas to locate online problems
- 2022.7.18-----leetcode.749
- An online accident, I suddenly realized the essence of asynchrony
- Vit:vision transformer super detailed with code
- Baidu website Collection
- 嵌入式系统移植【8】——设备树和根文件系统移植
- 文件上传到OSS文件服务器
- 【2016】【论文笔记】差频可调谐THz技术——
- Push to origin/master was rejected error resolution
猜你喜欢
![[H5 bottom scrolling paging loading]](/img/2c/fb8dd8a7d985392450ad7d3b70016c.png)
[H5 bottom scrolling paging loading]

Azure Synapse Analytics 性能优化指南(3)——使用具体化视图优化性能(下)

Vector execution engine framework gluten announced the official open source and appeared at spark technology summit

第二部分—C语言提高篇_12. 动/精态库的封装和使用
![[C language] classic recursion problem](/img/97/a88626e1a42f3f425396592a77100d.png)
[C language] classic recursion problem

Bid farewell to wide tables and achieve a new generation of Bi with DQL

第二部分—C语言提高篇_13. 递归函数

Qunar travel massive indicator data collection and storage

Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management

力扣141题:环形链表
随机推荐
【不积跬步无以至千里】统计日志指定时间段内的关键词
嵌入式系统移植【8】——设备树和根文件系统移植
[interview: concurrent Article 27: multithreading: hesitation mode]
动态sql
告别宽表,用 DQL 成就新一代 BI
企业数据治理面临的六大挑战!
数据供应链的转型 协调一致走向成功的三大有效策略
Galaxy securities online account opening commission, is online account opening safe for customer managers
【2016】【论文笔记】差频可调谐THz技术——
push to origin/master was rejected 错误解决方法
[Luogu] p2341 popular cattle
Can the stock account opening commission be adjusted? Is it safe to open an account on your mobile phone
Part II - C language improvement_ 11. Pretreatment
2. Realize the map of navigation bar and battle page
Tensorflow2.0 deep learning simple tutorial of running code
百度网址收录
第1章 拦截器入门及使用技巧
华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
Dynamic memory management and related topics
ES6新特性