This article shares the Vue calculation property implementation report card for your reference. The specific content is as follows The code is as follows: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Transcript Statistics</title> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <style type="text/css"> .gridtable{ font-family:verdana, arial, sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color:#666666; border-collapse: collapse; } .gridtable th{ border-width: 1px; padding:8px; border-style:solid; border-color:#666666; background-color: #dedede; } .gridtable td{ border-width: 1px; padding:8px; border-style:solid; border-color:#666666; background-color: #ffffff; } </style> </head> <body> <div id="app"> <table class="gridtable"> <tr> <th>Subject</th> <th>score</th> </tr> <tr> <td>Language</td> <td> <input type="text" name="" id="" value="" v-model.number="Chinese" /> </td> </tr> <tr> <td>Mathematics</td> <td> <input type="text" name="" id="" value="" v-model.number="Math" /> </td> </tr> <tr> <td>English</td> <td> <input type="text" name="" id="" value="" v-model.number="English" /> </td> </tr> <tr> <td>Total score</td> <td> <input type="text" name="" id="" value="" v-model.number="sum" /> </td> </tr> <tr> <td>Average score</td> <td> <input type="text" name="" id="" value="" v-model.number="average" /> </td> </tr> </table> </div> <script> var vm = new Vue({ el:"#app", data:{ English:100, Math:100, English:60 }, computed:{ sum:function(){ return this.Chinese+this.Math+this.English; }, average:function(){ return Math.round(this.sum/3); } }, }) </script> </body> </html> When I change the scores of Chinese, mathematics, and English, the total score and average score will change in real time. This is the characteristic of Vue's calculated properties. Vue computed property parameter passingA calculated property is essentially a method, but is usually used as a property, usually without (). However, in actual development, if you need to pass parameters to the method in the calculated attribute, you need to use the closure parameter passing method. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Evaluate</title> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <div id="app"> {{add(2)}} </div> <script type="text/javascript"> var vm = new Vue({ el:"#app", data:{ number:1 }, computed:{ add(){ return function(index){ return this.number+index; } } } }) </script> </head> <body> </body> </html> Notice:
Computed property getters and settersComputed properties are usually used to obtain data (change according to changes in data), so they only have getters by default, but Vue.js also provides setter functions when necessary. The order of the set method and the get method is independent of each other, and the parameter accepted by the set method is the return value of the get method. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Computed</title> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div id="app"> firstName:<input type="text" name="" id="" value="" v-model="first"/> lastName:<input type="text" name="" id="" value="" v-model="last"/> <p>fullName:<input type="text" name="" id="" value="" v-model="fullName"/></p> </div> <script type="text/javascript"> var vm = new Vue({ el:"#app", data:{ first:"Jack", last:"Jones" }, computed:{ fullName: get:function(){ return this.first+" "+this.last }, set:function(parameter){ var names = parameter.split(" ") this.first = names[0] this.last = names[names.length-1] } } } }) </script> </body> </html> The difference between computed properties and methodsThis approach of using computed properties ensures that code is only executed when necessary, making it suitable for handling potentially resource-intensive work. However, if the project does not have caching capabilities, methods should be used, depending on the actual situation. The characteristics of calculated properties are as follows: ①When the dependency of a calculated property changes, the calculation will be performed immediately and the calculation result will be automatically updated. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
>>: Detailed code for building a multi-person video chat service based on webrtc on Ubuntu
background Before we know it, a busy year is comi...
1. Search for redis image docker search redis 2. ...
Problem Description MySQL is started successfully...
inline-flex is the same as inline-block. It is a ...
This article will introduce how to use Docker to ...
Copy code The code is as follows: <style type=...
Install grafana. The official website provides an...
Install mysql5.7.18 on CentOS6.7 1. Unzip to the ...
WeChat Mini Program - QR Code Generator Download:...
When you use HTML div blocks and the middle of th...
You may already know that the length 1 of int(1) ...
Table of contents Preface Background Implementati...
Preface As a heavy user of front-end frameworks, ...
Table of contents 1. Isolation Level READ UNCOMMI...
Prelude We all know that nginx is an excellent re...