1. The following code is a simple usage of watch<div id="app"> <input type="text" v-model="firstName" /> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var vm = new Vue({ el:"#app", data:{ firstName:"张" }, watch:{ firstName:(newVal,oldVal){ //firstName is the name of the data you want to monitor. Use the function name you want to monitor, such as monitoring the firstName of v-model //newVal: indicates the value after the change, that is, the first parameter, do not swap positions //oldVal: indicates the value before the change, console.log({newVal,oldVal}); // {newVal: "陈", oldVal: "张"} } //Write a monitoring function directly, and execute the function every time the cityName value changes. //You can also add the method name in string format directly after the monitored data: firstName: 'nameChange' }, methods:{ nameChange(){ } } }) vm.firstName = "陈"; //Change the monitored value</script> 2. Immediate monitoringThere is a feature when using the basic usage of watch, that is, when the value is bound for the first time, the listening function will not be executed, and it will only be executed when the value changes. If we need to execute the function when the value is initially bound, we need to use the immediate attribute. watch: firstName: { handler(newName, oldName) { this.fullName = newName + ' ' + this.lastName; }, { immediate: true } } } The monitored data is written in object form, including the handler method and immediate. The function we wrote in the simple writing above is actually writing this handler method, which is omitted by default. 3. Handler method<div id="app"> <div> <p>Number: {{ myNumber }}</p> <p>Number: <input type="text" v-model.number="myNumber"></p> </div> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el: '#app', data: { myNumber: 'Dawei' }, watch: myNumber: { handler(newVal, oldVal) { console.log('newVal', newVal); console.log('oldVal', oldVal); }, //When immediate is true, the callback function is triggered immediately; if it is false, the callback will not be executed immediately, just like the example above. immediate: true } } }) </script> //The handler method is the specific method you need to execute in your watch 4. deep attributeHow do we monitor objects or properties in them? Then let's introduce the deep attribute. Its role is the key to solving this problem. <div id="root"> <p>obj.a: {{obj.a}}</p> <p>obj.a: <input type="text" v-model="obj.a"></p> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el: '#root', data: { obj: { a: 123 } }, watch: obj: { handler(newName, oldName) { console.log(newName, oldName); }, immediate: true, deep:true } } }) </script> If the above code is not added with deep:true, it will not be executed in the console. It will only be executed for the first time and the output result will be undefined. By default, the handler only monitors the reference changes of the obj property. It will only monitor when we assign a value to obj. At this time, you can use deep observation. The listener will traverse down layer by layer and add this listener to all properties of the object, but this is too time-consuming. SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: CSS3 uses the transition property to achieve transition effects
>>: How to install and persist the postgresql database in docker
Table of contents 1 Version and planning 1.1 Vers...
Download the rpm installation package MySQL offic...
CentOS 8 is officially released! CentOS fully com...
Table of contents Symbol Data Type The reason why...
The new project has basically come to an end. It ...
This article example shares the specific code of ...
First, the structure inside the nginx container: ...
Regarding uninstalling the previously installed v...
Installation environment: CAT /etc/os-release Vie...
The following three methods are commonly used to d...
count(*) accomplish 1. MyISAM: Stores the total n...
1. Create a test table CREATE TABLE `mysql_genara...
This article example shares the specific code of ...
HTML is the abbreviation of Hypertext Markup Langu...
I have always been interested in wireless interac...