计算属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div id="app">
    <table border="1">
        <thead>
            <th>学科</th>
            <th>分数</th>
        </thead>
        <tbody>
        <tr>
            <td>物理</td>
            <td><input type="text" v-model.number="physics"></td>
        </tr>
        <tr>
            <td>数学</td>
            <td><input type="text" v-model.number="math"></td>
        </tr>
        <tr>
            <td>英语</td>
            <td><input type="text" v-model.number="english"></td>
        </tr>
        <tr>
            <td>总分</td>
            <td>{{sum}}</td>
        </tr>
        <tr>
            <td>平均分</td>
            <td>{{avg}}</td>
        </tr>
        </tbody>
    </table>
</div>

<script src="../lib/vue.js"></script>
<script src="js/main.js"></script>
</body>
</html>
var app = new Vue({
    el:"#app",
    data:{
        math:90,
        physics:80,
        english:70,
    },
    computed:{
        sum:function () {
            return this.math+this.physics+this.english
        },
        avg:function () {
            return Math.round(this.sum/3)
        }
    }
})

 

posted @ 2018-01-30 17:44  一石数字欠我15w!!!  阅读(221)  评论(0编辑  收藏  举报