We're going to write a score sheet like this
| Mathematical |
90 |
| Physical |
80 |
| English |
70 |
<DivID= "App"><TableBorder= "1"><TR><TD>Mathematical</TD><TD>{{Math}}</TD></TR><TR><TD>Physical</TD><TD>{{Physics}}</TD></TR><TR><TD>English</TD><TD>{{中文版}}</TD></TR></Table></Div>
var New Vue ({ ' #app ', data : { english:7, $0 , } });
And then we're going to add the score and the average,
<TR> <TD>Score</TD> <TD>{{math + physics + 中文版}}</TD> </TR> <TR> <TD>Average score</TD> <TD>{{Math.Round (Math + physics + 中文版)/3}}</TD> </TR>
At this time we add a lot of things in the TD, this is not good, then Vue provides a computed attribute computed
For the convenience of observation we also change the contents of TR into input to observe dynamic changes.
<DivID= "App"> <TableBorder= "1"> <TR> <TD>Mathematical</TD> <TD><inputtype= "text"V-model= "Math"></TD> </TR> <TR> <TD>Physical</TD> <TD><inputtype= "text"V-model= "Physics"></TD> </TR> <TR> <TD>English</TD> <TD><inputtype= "text"V-model= "中文版"></TD> </TR> <TR> <TD>Score</TD> <TD>{{sum}}</TD> </TR> <TR> <TD>Average score</TD> <TD>{{Average}}</TD> </TR> </Table> </Div>
varApp =NewVue ({el:' #app ', data: {math:90, Physics:80, 中文版:70,}, computed: {sum:function(){ return This. Math + This. Physics + This. 中文版; }, Average:function() { returnMath.Round ( This. SUM/3); } } });
Add computed to the app then we can dynamically observe the scores of each branch and change the average score, but since input is automatically converted to a string type, we should ensure that it is the number type. We can use the number modifier of the V-model, or it can be processed on the data, V-model on the modifier is not said, in computed in the sum corresponding to the method of each data is added to a parsefloat converted to number,
computed: { function() { return parsefloat (this. Math) + Parsefloat (this. Physics) + parsefloat (this. 中文版) , function () { return math.round (this. sum/3); } }
At this time, change the results of each section can see the average score corresponding changes.
Vue.js Basic Learning Calculation attribute computed