Vue.js Basic Learning Calculation attribute computed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.