简单的购物车小案例

主要实现是:动态计算商品价格和数量,可以在添加或者减少商品时,显示总价。

主要练习点:vue的组件化的基础和组件传值,指令,computed方法

效果图:

 

代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .container {}
        
        .container .cart {
            width: 300px;
            margin: auto;
        }
        
        .container .title {
            background-color: lightblue;
            height: 40px;
            line-height: 40px;
            text-align: center;
            /*color: #fff;*/
        }
        
        .container .total {
            background-color: #FFCE46;
            height: 50px;
            line-height: 50px;
            text-align: right;
        }
        
        .container .total button {
            margin: 0 10px;
            background-color: #DC4C40;
            height: 35px;
            width: 80px;
            border: 0;
        }
        
        .container .total span {
            color: red;
            font-weight: bold;
        }
        
        .container .item {
            height: 55px;
            line-height: 55px;
            position: relative;
            border-top: 1px solid #ADD8E6;
        }
        
        .container .item img {
            width: 45px;
            height: 45px;
            margin: 5px;
        }
        
        .container .item .name {
            position: absolute;
            width: 90px;
            top: 0;
            left: 55px;
            font-size: 16px;
        }
        
        .container .item .price {
            position: absolute;
            width: 90px;
            top: 0;
            left: 110px;
            font-size: 16px
        }
        
        .container .item .change {
            width: 100px;
            position: absolute;
            top: 0;
            right: 50px;
        }
        
        .container .item .change a {
            font-size: 20px;
            width: 30px;
            text-decoration: none;
            background-color: lightgray;
            vertical-align: middle;
        }
        
        .container .item .change .num {
            width: 40px;
            height: 25px;
        }
        
        .container .item .del {
            position: absolute;
            top: 0;
            right: 0px;
            width: 40px;
            text-align: center;
            font-size: 40px;
            cursor: pointer;
            color: red;
        }
        
        .container .item .del:hover {
            background-color: orange;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container">
            <cart></cart>

        </div>
    </div>


    <template id='cart_list'>

    <div>
      <div class="item" v-for='(item,index) in list'>
        <img :src="item.img" />
        <div class="name"> {{item.name}}</div>
        <div class="price"> {{item.price}} </div>
        <div class="change">


          <!-- <a href="" @click.prevent="subEvent(item,index)">-</a>
          <input type="text" class="num" :value="item.num" @change='changeNum(item,$event)' />
          <a href="" @click.prevent="addEvent(item,index)">+</a> -->

          <a href="" @click.prevent="changeEvent(item,index,'sub',$event)">-</a>
          <input type="text" class="num" :value="item.num" @change="changeEvent(item,index, 'change', $event)" />
          <a href="" @click.prevent="changeEvent(item,index,'add',$event)">+</a>
        </div>
        <div class="del" @click='delData(index)'>×</div>
      </div>

    </div>

  </template>



    <template id='titles'>
    <h1 class="title"> {{title}}</h1>
  </template>

    <template id='total'>
    <div class="total">
      <span>总价格: {{totalPrice}}</span>
      <button>结算</button>
    </div>
  </template>



    <script type="text/javascript" src="./vue.js"></script>
    <script type="text/javascript">
        let cartList = [{
            "img": "./img/a.jpg",
            "name": "TCL",
            "price": 2000,
            "num": 5
        }, {
            "img": "./img/b.jpg",
            "name": "xiaomi",
            "price": 1999,
            "num": 1
        }, {
            "img": "./img/c.jpg",
            "name": "Haier",
            "price": 1999,
            "num": 1
        }, {
            "img": "./img/e.jpg",
            "name": "pptv",
            "price": 1999,
            "num": 1
        }]

        // 定义三个基础组件 
        // 购物车商品标题组件
        var titles = {
            template: "#titles",
            props: ['title']

        }

        //购物车商品展示组件
        var cart_list = {
            template: "#cart_list",
            // 接受 cart 组件传递过来的数据
            props: ['list'],
            methods: {
                //删除功能  因为我们的数据是父组件传递过来的 所以我们遵循单向数据流 我们不能直接更改父组件传递过来的数据
                delData(index) {
                    // alert(index)
                    this.$emit('delGoods', index)
                },


                changeEvent(item, index, type, event) {


                    let obj = {
                        type,
                        index,
                        item,
                        num: event.target.value * 1

                    }

                    this.$emit('changeGoods', obj)

                }




            }




        }




        //购物车计算总价格组件
        var total = {
            template: '#total',
            props: ['list'],

            computed: {


                totalPrice() {
                    return this.list.reduce((cur, current) => cur + current.price * current.num, 0)
                },



            }

        }



        //定义最外层组件
        Vue.component('cart', {
            template: `
       <div>
            <titles  :title='title'></titles>
    
            <cart_list :list='list'  @delGoods='deleteGoods'  @changeGoods='changeCartGoods'    
            ></cart_list>
            <total  :list='list'></total>
        </div>
      `,
            data() {
                return {
                    list: [],
                    title: '购物车标题'

                }
            },
            methods: {
                //删除功能
                deleteGoods(i) {
                    // alert(i)
                    let currentIndex = this.list.findIndex((item, index) => index == i)
                    this.list.splice(currentIndex, 1)

                },
                changeCartGoods({
                    index,
                    item,
                    num,
                    type
                }) {


                    if (type == 'add') {
                        this.list.forEach((item, i) => {
                            if (i == index) {
                                item.num += 1
                            }
                            return
                        })
                    } else if (type == 'sub') {
                        this.list.forEach((item, i) => {
                            if (i == index) {
                                item.num -= 1

                                if (item.num <= 0) {
                                    item.num = 1
                                    alert('不能再减了')

                                    return

                                }
                            }
                        })
                    } else {
                        this.list.forEach((item, i) => {
                            if (i == index) {
                                item.num = num
                                return
                            }
                        })

                    }
                }

            },

            mounted() {
                this.list = cartList
            },

            components: {
                titles,
                cart_list,
                total
            }
        })



        var vm = new Vue({
            el: '#app',
            data: {




            },


        });
    </script>
</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值