Vue第一周实现购物车小案例分享

相信大家一路从原生js过来一路非常的不容易学到最后越来越简单,Vue第一周的购物车小案例分享给大家,祝大家学的开心月薪过万!~~~

下面是效果图

下面是代码:

<div id="app">
        <table border="1">
            <tr>
                <th></th>
                <th>名称</th>
                <th>价格</th>
                <th>数量</th>
            </tr>
            <tr v-for="item in goods" :key="item.name">//这里遍历goods的数据
                <td><input type="checkbox" v-model="item.ad"></td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td><button @click="item.num++">{{item.num}}</button></td>
            </tr>
        </table>
        <p>
            总价:{{yy}}一共:{{cc}}件
        </p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script>
        new Vue({
            el: "#app",
            computed: {
                yy() {
                    //定义一个为0的变量
                    var j = 0;
                    this.goods.forEach(item => {
                        //如果ad为true才计算
                        if (item.ad) {
                            //总计就等于数量乘单价
                            j += item.num * item.price
                        }
                    })
                    return j
                },
                cc() {
                    var j = 0;
                    this.goods.forEach(item => {
                        if (item.ad) {
                            j = item.num + j
                        }
                    })
                    return j
                }
            },
            data() {
                return {
                    goods: [{
                        //书名等数据
                        name: "js入门", price: 20, num: 1, ad: true
                    },
                    {
                        name: "vue入门", price: 15.5, num: 2, ad: false,
                    }
                    ]
                }
            }
        })
    </script>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值