vue实现购物车逻辑

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

<head>
    <meta charset="UTF-8">
    <title>index</title>
    <style>
        table {
            border: 1px solid #e9e9e9;
            border-collapse: collapse;
            border-spacing: 0;
        }

        th,
        td {
            padding: 8px 16px;
            border: 1px solid #e9e9e9;
            text-align: left;
        }

        th {
            background-color: #f7f7f7;
            color: #5c6b77;
            font-weight: 600;
        }
    </style>
</head>

<body>

    <div id="app">
        <!-- 如果list有内容,渲染购物车内容 -->
        <div v-if="list.length">
            <table>
                <thead>
                    <tr>
                        <!-- id -->
                        <th></th>
                        <th>书籍名称</th>
                        <th>出版日期</th>
                        <th>价格</th>
                        <th>购买数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- 给tr写入list的循环  -->
                    <!-- 不建议直接遍历整个对象,而是用item.name之类的方式写入数据,方便加入+-的按钮等操作 -->
                    <!-- 获取index是很重要der -->
                    <tr v-for="(item, index) in list" :key="item.id">
                        <!-- index每次循环+1 -->
                        <td>{{index + 1}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.date}}</td>
                        <!-- 价格建议保留两位小数,数据有.00 但渲染的时候被省略掉了 -->
                        <td>{{item.price|showPrice}}</td>
                        <!-- 可以直接让显示的内容保留两位小数点
                        由于总价有同样的需求,可以封装函数
                        -->
                        <!-- <td>{{'¥' + item.price.toFixed(2)}}</td> -->

                        <td>
                            <!-- 如果count小于等于0 就禁用button -->
                            <!-- 给加减的button分别注册点击加减数字的事件 -->
                            <button :disabled="item.count<=0" @click="increment(index)">-</button>
                            <span>{{item.count}}</span>
                            <button @click="decrement(index)">+</button>
                        </td>
                        <td>
                            <!-- 点击移除事件 -->
                            <button @click="removeItem(index)">移除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <!-- 计算并渲染总价(保留两位小数点并拼接¥符号) -->
            <h2>总价格: {{totalPrice | showPrice}}</h2>
        </div>
        <!-- 如果购物车莫得东西,显示这个 -->
        <div v-else>购物车没有书籍!</div>
    </div>
    <script src="https://cdn.bootcss.com/vue/2.6.9/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                // books 的list
                list: [{
                        id: 1,
                        name: '《算法导论》',
                        date: '2006-9',
                        price: 85.00,
                        count: 1
                    },
                    {
                        id: 2,
                        name: '《UNIX编程艺术》',
                        date: '2006-2',
                        price: 59.00,
                        count: 1
                    },
                    {
                        id: 3,
                        name: '《编程珠玑》',
                        date: '2008-10',
                        price: 39.00,
                        count: 1
                    },
                    {
                        id: 4,
                        name: '《代码大全》',
                        date: '2006-3',
                        price: 128.00,
                        count: 1
                    },
                ]
            },
            computed: {
                // 计算总价
                totalPrice() {
                    // 箭头函数
                    return this.list.reduce((preValue, item) => {
                        // 单价乘数量
                        // preValue是之前已经添加的价格 item是(回调中的array)是list中每一项
                        // reduce(callback,innitialValue)
                        // callback对数组中的所有元素调用指定的回调函数。 返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
                        // callback 如果提供了 initialValue, 则 reduce 会对数组中的每个元素调用一次 callback.如果未提供 initialValue,则 reduce方法会对从第二个元素开始的每个元素调用 callbackfn 函数。
                        // 回调函数语法 function callbackfn(previousValue, currentValue, currentIndex, array1)
                        return preValue + item.price * item.count
                        // 0是初始值
                    }, 0)
                }
            },
            filters: {
                // 过滤器 这个东西放在methods里不如放在过滤器里
                // 过滤器不改变真正的data,而只是改变渲染的结果,并返回过滤后数据
                showPrice(price) {
                    return '¥' + price.toFixed(2)
                }
            },
            methods: {
                // 增加和减少让count++ -- ,然后vue会自动把变化的数据渲染到页面
                //根据index判断要改的哪一行的count
                increment(index) {
                    this.list[index].count--
                    // console.log(index);
                },
                decrement(index) {
                    this.list[index].count++
                },
                // 点击移除时,获取当前行index  splice(要移除的位置,移除的列数),移除当前list行
                removeItem(index) {
                    this.list.splice(index, 1)
                }
            },
        })
    </script>
</body>

</html>

注释写的挺全的,所以就不做过多说明了~~

博客地址 :https://www.cnblogs.com/sandraryan/

猜你喜欢

转载自www.cnblogs.com/sandraryan/p/11973312.html