7.计算属性computed

<div id="calcu">
    <table border="1px solid black" style="text-align: center" align="center">
        <tr>
            <th>水果</th>
            <th>数量</th>
            <th>单价(元/个)</th>
            <th>预计收入</th>
        </tr>
        <tr>
            <td>苹果</td>
            <td><input class="in" v-model.number="apple[0]"></td>
            <td><input class="in" v-model.number="apple[1]"></td>
            <td>{{sum_app}}</td>
        </tr>
        <tr>
            <td>梨</td>
            <td><input class="in" v-model.number="pear[0]"></td>
            <td><input class="in" v-model.number="pear[1]"></td>
            <td>{{sum_pear}}</td>
        </tr>
        <tr>
            <td>桃子</td>
            <td><input class="in" v-model.number="peach[0]"></td>
            <td><input class="in" v-model.number="peach[1]"></td>
            <td>{{sum_peach}}</td>
        </tr>
        <tr>
            <td>合计</td>
            <td>{{sum_num}}</td>
            <td>----</td>
            <td>{{sum_price}}</td>
        </tr>
    </table>
</div>

new Vue({
    el:'#calcu',
    data:{
        apple:[25,3],
        pear:[30,1],
        peach:[22,0.8]
    },
    computed:{
        sum_app:function () {
            return this.apple[0] * this.apple[1];
        },
        sum_pear:function () {
            return this.pear[0] * this.pear[1];
        },
        sum_peach:function () {
          return this.peach[0] * this.peach[1];
        },
        sum_num:function () {
            return this.apple[0]+this.pear[0]+this.peach[0];
        },
        sum_price:function () {
            return this.sum_app+this.sum_pear+this.sum_peach;
        }
    }
})

猜你喜欢

转载自blog.csdn.net/qq_26703533/article/details/80822239