This time I will show you how to use watch in Vue, and what are the precautions for using watch in Vue. The following is a practical case, let’s take a look.
Assume the following code:<p>
<p>FullName: {{fullName}}</p>
<p>FirstName: <input type="text" v-model="firstName"></p>
</p>
new Vue({
el: '#root',
data: {
firstName: 'Dawei',
lastName: 'Lou',
fullName: ''
},
watch: {
firstName(newName, oldName) {
this.fullName = newName + ' ' + this.lastName;
}
}
})watch: {
firstName: {
handler(newName, oldName) {
this.fullName = newName + ' ' + this.lastName;
},
// 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
immediate: true
}
}deep attribute
There is also an attribute deep in watch. The default value is false, which represents whether to monitor deeply. For example, there is an obj attribute in our data:
<p>
<p>obj.a: {{obj.a}}</p>
<p>obj.a: <input type="text" v-model="obj.a"></p>
</p>
new Vue({
el: '#root',
data: {
obj: {
a: 123
}
},
watch: {
obj: {
handler(newName, oldName) {
console.log('obj.a changed');
},
immediate: true
}
}
})event hook function. Reassign:
mounted: {
this.obj = {
a: '456'
}
}watch: {
obj: {
handler(newName, oldName) {
console.log('obj.a changed');
},
immediate: true,
deep: true
}
}stringform monitoring.
watch: {
'obj.a': {
handler(newName, oldName) {
console.log('obj.a changed');
},
immediate: true,
// deep: true
}
}Using JS to determine the content contained in a string Summary of methods
Angular RouterLink makes different flowers Type jump
The above is the detailed content of How to use watch in Vue. For more information, please follow other related articles on the PHP Chinese website!
What is the difference between webstorm and idea?
How to check deleted call records
HTML image code collection
How much is Dimensity 8200 equal to Snapdragon?
What are the methods for detecting ASP vulnerabilities?
The difference between Java and Java
ERR_CONNECTION_REFUSED
How to enable secondary logon service