PrefaceVue provides a more general way to respond to data changes through the watch option. This approach is most useful when you need to perform asynchronous or expensive operations when data changes. 1. Basic usage of listener<div id="demo">{{ fullName }}</div> var vm = new Vue({ el: '#demo', data: { firstName: 'Foo', lastName: 'Bar', fullName: 'Foo Bar' }, watch: firstName: function (val) { this.fullName = val + ' ' + this.lastName }, lastName: function (val) { this.fullName = this.firstName + ' ' + val } } }) The purpose of this code is to monitor firstName and fullName, and when they change, change the value of fullname. 2. Listener FormatListener in method format
Object format listener
3. Trigger monitoring and deep monitoring as soon as the page is enteredTrigger the listener as soon as you enter the page
const vm = new Vue({ el: '#app', data: { info: username: 'admin' } }, watch:{ info: handle(newVal){ console.log(newVal) }, // Trigger the listener as soon as the page is entered immediate: true } } }) Deep MonitoringIn the above code, we cannot monitor successfully when username changes, because what changes is the value of the object attribute, so we need deep monitoring, just add the deep option const vm = new Vue({ el: '#app', data: { info: username: 'admin' } }, watch:{ info: handle(newVal){ console.log(newVal) }, // Trigger the listener as soon as the page is entered immediate: true, // Implement deep monitoring. As long as any property of the object changes, the "object monitoring" will be triggered. deep: true } } }) Deep listening returns the value of the sub-property of the listening objectThe execution result of the above code is to print the info object. We want to print the value of username, but it is troublesome to add newVal.username. We can actually directly monitor and print the value of the changed sub-attribute. We only need to add a layer of single quotes around the sub-attribute to be monitored: const vm = new Vue({ el: '#app', data: { info: username: 'admin' } }, watch:{ 'info.username': { handle(newVal){ console.log(newVal) } } } }) at last⚽This article introduces the basic use of listeners in Vue and how to implement deep listening. I hope you will gain something after reading it~ This concludes this article on the basic usage of listeners in Vue. For more information on the usage of Vue listeners, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the idea of distributed lock in MySQL with the help of DB
>>: Vue-cli framework implements timer application
Table of contents (I) Using Workbench to operate ...
The first article on data backup and restoration ...
Our veteran predecessors have written countless c...
Ping www.baidu.com unknown domain name Modify the...
This is not actually an official document of IE. I...
This article example shares the specific code of ...
Preface For production VPS with public IP, only t...
There are several ways I know of to set anchor pos...
Solution to "Could not run curl-config"...
Excel export always fails in the docker environme...
Table of contents 1. What is a custom instruction...
This article example shares the specific code of ...
1. Explanation of provide and inject Provide and ...
Perfect solution to the scalable column problem o...
With the right settings, you can force Linux user...