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 Using routing plugins in a modu...
This article shares the specific code of videojs+...
Preface Linux's file permission management is...
Now, more and more front-end developers are starti...
Preface The solution to the problem of not being ...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents Vue life cycle introduction and...
We hope to insert the weather forecast into the w...
Preface Sometimes file copies amount to a huge wa...
The mini program implements a complete shopping c...
top command is the best command that everyone is ...
The communication modes of vue3 components are as...
Database modification or deletion operations may ...
This article shares the specific code for JavaScr...
need: The official website's resource server ...