1. Computed properties and listeners1.1 Computed properties<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <p>{{ message }}</p> <p>{{ reversedMessage }}</p> </div> <script> new Vue({ el: '#app', data: { message: 'hello' }, computed: { reversedMessage: function () { return this.message.split('').reverse().join('') } /* // equivalent to reversedMessage: { get(){ return this.message.split('').reverse().join('') } } */ } }); </script> </body> </html> Explanation: We defined the computed property reversedMessage in the computed property. The function provided here will be used as the getter function of the computed property reversedMessage. 1.2 SettersComputed properties have only a getter by default, but we can provide a setter. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <p>{{ message }}</p> <p>{{ reversedMessage }}</p> <input type="text" v-model="reversedMessage" /> </div> <script> new Vue({ el: '#app', data: { message: 'hello' }, computed: { reversedMessage: { get(){ return this.message.split('').reverse().join('') }, set(value){ this.message = value.split('').reverse().join('') } } } }); </script> </body> </html> 1.3 Caching<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <p>{{ message }}</p> <p>{{ reversedMessage }}</p> <p>{{ reversedMessage1() }}</p> </div> <script> new Vue({ el: '#app', data: { message: 'hello' }, methods: { reversedMessage1: function(){ return this.message.split('').reverse().join('') } }, computed: { reversedMessage: function () { return this.message.split('').reverse().join('') } } }); </script> </body> </html> Note: Although the same effect can be achieved with both computed properties and methods, computed properties are cached based on their reactive dependencies. They are only re-evaluated when the associated reactive dependencies change. 1.4 Listening propertiesYou can listen to data changes through the watch property of the Vue instance. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <p>{{ message }}</p> <button @click="reverse=!reverse">Reverse</button> </div> <script> new Vue({ el: '#app', data: { message: 'Vue', reverse: false }, watch: // message: function(newVal, oldVal){ reverse: function(newVal){ console.log(newVal) this.message = this.message.split('').reverse().join('') } }, }); </script> </body> </html> We can achieve the same effect through the instance attribute vm.$watch. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <p>{{ message }}</p> <button @click="reverse=!reverse">Reverse</button> </div> <script> var vm = new Vue({ el: '#app', data: { message: 'Vue', reverse: false } }); // vm.$watch('reverse', function (newVal, oldVal) { vm.$watch('reverse', function (newVal) { console.log(newVal) this.message = this.message.split('').reverse().join('') }); </script> </body> </html> The above is the detailed content of the summary of the use of Vue calculated properties and listeners. For more information about Vue calculated properties and listeners, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to manage docker through UI
>>: MySQL implements a function similar to connect_by_isleaf MySQL method or stored procedure
Contemporary web visual design has gone through th...
How to quickly copy a table First, create a table...
Install linux7.2 Internet access configuration on...
The webpage displays 403 Forbidden Nginx (yum ins...
Servermanager startup connection database error R...
Grammatical rules SELECT column_name(s) FROM tabl...
How to change the image hyperlink when the mouse p...
The key features of the InnoDB storage engine inc...
Table of contents Preface What are asynchronous i...
Let's first look at some simple data: Accordin...
background A specific device is used to perform i...
IE8 will have multiple compatibility modes . IE pl...
Table of contents introduction 1. Case Overview 2...
The first step is to create a mysql container doc...
Table of contents 1. Introduction 2. Solution Imp...