01. Listener watch (1) Function
export default { data() { return { number: 1 } }, watch:{ // Ordinary monitoring method, here it means monitoring the number attribute in data // The first parameter indicates the new value after the change, and the second parameter indicates the old value before the change number(newVal,oldVal){ console.log(newVal); console.log(oldVal); } } } (2) Properties and methods
export default { data(){ return { number: 1 } }, watch: // Listen for the number attribute number: { handler(newVal, oldVal){ }, immediate: true, // Listen immediately} } } (3) Monitoring object
export default { data() { return { obj: { a: 1 } } }, watch: obj: { handler(newVal){ console.log('Listened to', newVal) }, immediate: true } }, created(){ // Cannot be monitored because it is a modification operation on the attribute // Print once, and the print result is the modified value, this.obj.a = 2 // It can be monitored because it is a direct assignment operation on the object // Print twice (immediate monitoring will print once, and modification will print once) this.obj = { a: 2} } }
export default { watch: 'obj.a': { handler(newVal){ console.log(newVal) } } }, created(){ // Both of the following can be monitored and printed twice this.obj.a = 2 this.obj = { a:2 } } }
export default { watch: obj: { handler(newVal){ console.log(newVal) }, deep: true, immediate: true } }, created(){ // After deep monitoring, changes in attributes can also be monitored directly // Print twice (because of immediate) this.obj.a = 2 // Unable to monitor the addition of object properties // Print once, and the print result is the object with the newly added properties // That is, it will only be executed once due to immediate, and print out {a:1,b:2} this.obj.b = 2 // The monitoring can be triggered, but the changes cannot be monitored // Printed twice, both values are {a:2}, which cannot reflect the changes this.$set(this.obj, 'a', 2) } } (4) Listening array
export default { data() { return { arr: [1] } }, watch: arr: { handler(newVal, oldVal) { console.log('New:', newVal) console.log('old:', oldVal) }, immediate: true } }, created() { // Can be monitored --- directly assign the entire array this.arr = [2] // Unable to monitor --- index assignment, length modification this.arr[1] = 2 this.arr[0] = 2 this.arr.length = 2 // Can trigger monitoring, but cannot monitor changes => the new and old values are the same this.arr.push(2) this.$set(this.arr, 0, 2) } } 02. Computed properties (1) Set method for computing attributes
computed: { fullName: get () { return `${this.firstName} ${this.lastName}`; }, set (val) { const names = val.split(' '); this.firstName = names[0]; this.lastName = names[names.length - 1]; } } }
(2) Difference
(3) Usage scenarios
The above is a detailed summary of the use of vue Watch and Computed. For more information on the use of vue Watch and Computed, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: MySQL 5.7.18 Installer installation download graphic tutorial
>>: How to deploy nginx with Docker and modify the configuration file
Table of contents 1. Retrieve the image 2. Create...
This article describes how to use the local yum s...
How to modify the mysql table partitioning progra...
Anyone who has used the Linux system should know ...
When setting display:flex, justify-content: space...
vmware vsphere 6.5 is the classic version of vsph...
The cut command in Linux and Unix is used to cu...
<br />The frame structure allows several web...
Table of contents 1. Simple page example 2.uni-ap...
Download the rpm installation package MySQL offic...
This article example shares the specific code for...
A simple license plate input component (vue) for ...
To execute a shell command in Docker, you need to...
In the previous article, we learned about the net...
Table of contents 1. Simple mounting of persisten...