【vue】购物车简单demo

代码:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</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">
        <div v-if='books.length'>
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th>书籍</th>
                        <th>出版日期</th>
                        <th>价格</th>
                        <th>购买数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for='(book, index) in books'>
                        <td>{{book.id}}</td>
                        <td>{{book.title}}</td>
                        <td>{{book.publish}}</td>
                        <td>{{book.price | showPrice}}</td>
                        <td>
                            <button @click='decrement(index)' :disabled='book.count <= 1'>-</button>
                            {{book.count}}
                            <button @click='increment(index)'>+</button>
                        </td>
                        <td><button @click='removeBook(index)'>移除</button></td>
                    </tr>
                </tbody>
            </table>
        <h3>总价格:{{totalPrice | showPrice}}</h3>
    </div>
    <h3 v-else>购物车为空</h3>
    </div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    const app = new Vue({
        el: '#app',
        data: {
            books: [
                {id: 1, title: '算法导论', publish: '2020-01', price: 10, count: 1},
                {id: 2, title: '数据结构', publish: '2020-02', price: 20, count: 1},
                {id: 3, title: '代码大全', publish: '2020-03', price: 30, count: 1},
                {id: 4, title: '删库跑路', publish: '2020-04', price: 40, count: 1}
            ]
        },
        methods: {
            decrement(index) {
                this.books[index].count--
            },
            increment(index) {
                this.books[index].count++
            },
            removeBook(index) {
                this.books.splice(index, 1)
            }
        },
        computed: {
            totalPrice() {
                // 1.普通for循环
                // let totalPrice = 0;
                // for (let i = 0; i < this.books.length; i++) {
                //     totalPrice += this.books[i].price * this.books[i].count
                // }
                // return totalPrice

                // 2.for in
                // let totalPrice = 0;
                // for (let i in this.books) {
                //     totalPrice += this.books[i].price * this.books[i].count
                // }
                // return totalPrice

                // 3.for of
                // let totalPrice = 0;
                // for (let book of this.books) {
                //     totalPrice += book.price * book.count
                // }
                // return totalPrice

                // reduce
                return this.books.reduce((preValue, book) => {
                    return preValue + book.price * book.count
                }, 0)
            }
        },
        filters: {
            showPrice(price) {
                return '¥' + price.toFixed(2)
            }
        }
    })
</script>
</body>
</html>

效果:

posted @ 2020-10-02 14:22    阅读(219)  评论(0编辑  收藏  举报