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
In the previous article, I introduced the detaile...
Written in front Environment: MySQL 5.7+, MySQL d...
Table of contents 1. Concept 2. Environmental Des...
Table of contents 1. Four concepts 1. JavaScript ...
1. Basic lines 2. Special effects (the effects ar...
In Vue, we generally have front-end and back-end ...
Preface: In the daily use of the database, it is ...
Solution: Just set the link's target attribute...
Table of contents 1. What is Dockerfile? 2. Analy...
Table of contents 1. Understanding Queues 2. Enca...
Table of contents Principle Source code analysis ...
How to obtain SQL statements with performance iss...
Preface This article will focus on the use of Typ...
Preface I wrote a small demo today. There is a pa...
Imagine a scenario where, when designing a user t...