• 购物车实现二


    直接使用computed计算属性来改变总价

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <title>Title</title>
        <!-- Bootstrap -->
        <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
        <script type="text/javascript" src="./node_modules/vue-router/dist/vue-router.js"></script>
        <script type="text/javascript" src="./node_modules/axios/dist/axios.js"></script>
    
    </head>
    <body>
    <div id="box">
        <div class="text-info"><h4>购物车</h4></div>
        <rk-cart :goods="goods"></rk-cart>
    
        <div>
            合计:{{totalPrice}}元
        </div>
    
    </div>
    
    <script type="text/x-template" id="cart">
        <table border="1" width="90%">
            <tr>
                <th>序号</th>
                <th>名称</th>
                <th>价格</th>
                <th>数量</th>
                <th>操作</th>
            </tr>
    
            <tr v-for="(item,key) in goods">
                <td>{{item.id}}</td>
                <td>{{item.goodsName}}</td>
                <td>{{item.price}}</td>
                <td>
                    <input type="text" v-model="item.num">
                </td>
                <td>删除</td>
            </tr>
        </table>
    </script>
    
    <script type="text/javascript">
    
        let rkCart = {
            props:['goods'],
            template:'#cart'
        };
    
        let app = new Vue({
            el:'#box',
            computed:{
                totalPrice(){
    
                    let sum = 0;
                    this.goods.forEach(v=>{
                        sum += v.price * v.num;
                    });
                    return sum;
    
                }
            },
            data:{
                goods:[
                    {id:1,goodsName:'iphone 8 Plus',price:6900,num:1},
                    {id:2,goodsName:'爱奇艺vip',price:100,num:1},
                ],
            },
            components:{rkCart},
        });
    </script>
    </body>
    </html>
    

      

  • 相关阅读:
    linux redis 安装和链接,,,
    ppt转化pdf
    跨服务器 同步数据
    字典表相关
    代码重构,空间换时间,dictionary 不要用object ,需明确指定类型
    stringbuilder for test performance 性能 update 性能
    nvarchar 和varchar 在len下一致,datalength下nvarchar翻倍
    android GradLayout实现计算器
    屏幕录制GIF动画工具
    android SharedPreferences 简单的数据存储
  • 原文地址:https://www.cnblogs.com/wntd/p/9051284.html
Copyright © 2020-2023  润新知