zoukankan      html  css  js  c++  java
  • 43_2.VUE学习之--不使用组件computed计算属性超简单的实现美团购物车原理

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
    <div id="hdcms">
        <!-- .sync 当数据改变时,会自动把子组件里的值赋值到父组件goods里,当goods的值改变时,totalPrice又会重新计算-->
        <table border="1" width="90%">
            <tr>
                <th>商品名称</th><th>商品价格</th><th>商品数量</th><th>合计</th>
            </tr>
            <tr v-for="(v,k) in goods">
                <td>{{v.title}}</td>
                <td><input type="text" v-model="v.price"></td>
                <td>
                    <input type="text" v-model="v.num">
                </td>
                <td>{{v.price*v.num}}</td>
            </tr>
        </table>
        总计:{{totalPrice}}元
    </div>
    <script type="text/x-template" id="hdNews">
    
    </script>
    <script>
    
        new Vue({
            el: '#hdcms',
            components: {hdNews},
            //页面加载完后,加自动执行
            computed:{
                totalPrice(){
                    var sum=0;
                    this.goods.forEach((v)=>{
                        sum+=v.num*v.price;
                    })
                    return sum;
                }
            },
            data: {
                goods:[
                    {title:'iphone7Plus',price:100,num:1},
                    {title:'后盾人会员',price:200,num:1},
                ]
            }
        });
    </script>
    </body>
    </html>
    

    效果:

  • 相关阅读:
    数据库索引的作用和长处缺点
    Spring的依赖注入
    Spring+Ibatis集成开发实例
    IOS开发之类和对象
    关于C++ const 的全面总结
    包管理器Bower使用手冊之中的一个
    项目总算完工了
    git reset and git checkout
    unity3D中协程和线程混合
    【剑指offer】左旋转字符串
  • 原文地址:https://www.cnblogs.com/haima/p/10275076.html
Copyright © 2011-2022 走看看