1. watch monitoring propertiesThis property is used to monitor changes in certain data and trigger the execution of the corresponding callback function. Basic usage: add The callback function has 2 parameters:
counter: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <div>Counter {{ shu }}</div> <span>Before change: {{ov}}</span> <span>After change: {{nv}}</span> <br /> <button @click="shu++">Add one</button> </div> <script> var vm = new Vue({ el: '#app', data: { Shu:1, ov:0, nv:0 }, methods:{ } }) vm.$watch("shu",function(nval,oval){ vm.$data.ov = oval vm.$data.nv = nval }) </script> </body> </html> Add a listener and assign the value of the counter before it changes to the variable vm.$watch("shu",function(nval,oval){ vm.$data.ov = oval vm.$data.nv = nval })
1. Shopping cart<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <table> <tr> <th>Serial number</th> <th>Product Name</th> <th>Product price</th> <th>Purchase quantity</th> <th>Operation</th> </tr> <tr v-for="sp in sps"> <td>{{ sp.id }}</td> <td>{{ sp.name }}</td> <td>{{ sp.money }}</td> <td> <button v-on:click="sp.sum=sp.sum-1">-</button> {{ sp.sum }} <button v-on:click="sp.sum=sp.sum+1">+</button> </td> <td> <button v-on:click="sp.sum=0">Reset</button> </td> </tr> </table> <div > Total price: {{ getmany() }} </div> </div> <script> var vm = new Vue({ el: '#app', data: { sps:[ { id:1, name:"Apple 13", money:6000, sum:1 }, { id:2, name:"Apple 12", money:5000, sum:1 }, { id:3, name:"Apple 11", money:4000, sum:1 } ] }, methods:{ getmaney:function(){ var m=0; for(var x=0;x<this.sps.length;x++){ m=m+this.sps[x].money*this.sps[x].sum; } return m; } } }) </script> </body> </html> The total cost is: getmaney:function(){ var m=0; for(var x=0;x<this.sps.length;x++){ m=m+this.sps[x].money*this.sps[x].sum; } return m; } 2. Select all and deselect all<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <input type="checkbox" id="a" value="a" v-model="che"/> <label for="a">a</label> <input type="checkbox" id="b" value="b" v-model="che"/> <label for="b">b</label> <input type="checkbox" v-model="checked" id="bok" v-on:change="ckall()" /> <label for="box">Select All</label> </div> <script> var vm = new Vue({ el: '#app', data: { checked:false, che:[], shuzu:["a","b"] }, methods:{ ckall:function(){ //Select all status if(this.checked){ this.che = this.shuzu }else{ //Cancel all selections this.che=[] } } }, watch:{ "che":function(){ //Judge whether to select all if(this.che.length == this.shuzu.length){ this.checked = true }else{ this.checked = false } } } }) </script> </body> </html> 2. Calculated properties 1.computedFeatures:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <p>{{ mess }}</p> <p>{{ remess }}</p> <p>{{ mess.split('').reverse().join('') }}</p> </div> <script> var vm = new Vue({ el: '#app', data:{ mess:'abcd' }, computed: { remess:function(){ return this.mess.split('').reverse().join('') } } }) </script> </body> </html> 2.methods <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <input v-model="mess" /> <p>{{ mess }}</p> <p>{{ remess }}</p> <p>{{ remess2() }}</p> </div> <script> var vm = new Vue({ el: '#app', data:{ mess:'abcd' }, computed: { remess:function(){ return this.mess.split('').reverse().join('') } }, methods: { remess2:function(){ return this.mess.split('').reverse().join('') } } }) </script> </body> </html> 3. Setters When the page obtains certain data, it will first look in the data. If it cannot find the data, it will look in the calculated attribute. When obtaining data in the calculated attribute, the get method will be automatically executed, and a set method is also provided. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <p>{{ site }}</p> </div> <script> var vm = new Vue({ el: '#app', data: { name: 'xiaowang', cls:'01' }, computed:{ site:{ get: function(){ return this.name+' '+this.cls }, set: function(Value){ var names = Value.split('|') this.name = names[0] this.cls = names[1] } } } }) vm.site = 'xiaowang|01'; document.write('name:'+vm.name); document.write('<br>'); document.write('class:'+vm.cls); </script> </body> </html> This is the end of this article about Vue listening properties and calculated properties. For more relevant Vue listening and calculated properties content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Enter the style file for the automatic search suggestion function: suggestion.css
>>: CSS achieves the effect of two elements blending (sticky effect)
First, let's introduce several common operati...
View installation and uninstallation # View rpm -...
When using the <html:reset> tag, sometimes w...
Preface: This article only introduces the steps t...
Before using idea to write JSP files, you need to...
This article shares the specific code for JavaScr...
Table of contents Preface first step: Step 2: Mod...
Table of contents introduction 1. Case Overview 2...
Today I will talk about a CSS special effect of h...
Table of contents Environmental Description Insta...
Preface Most of our MySQL online environments use...
environment: 1. CentOS6.5 X64 2.mysql-5.6.34-linu...
Passing values between mini program pages Good ...
This article example shares the specific code of ...
Table of contents The creation and confusion of n...