Vue练习:书籍购物车

1.效果图

2.案例说明

 1.在界面上以表格的形式,显示一些书籍的数据;

 2.在底部显示书籍的总价格;

 3.点击+或者-可以增加或减少书籍数量(如果为1,那么不能继续-);

 4.点击移除按钮,可以将书籍移除(当所有的书籍移除完毕时,显示:购物车为空~);

3.开发过程

制作表单头部:

            <tr>
                <td></td>
                <td>书籍名称</td>
                <td>出版日期</td>
                <td>价格</td>
                <td>购买数量</td>
                <td>操作</td>
            </tr>

定义一个Vue实例且挂在到一个div域上:

  <div id="app">

  </div>

  <script>
    // 1.创建app
    const app = Vue.createApp({
      data: function() {
        return {
        }
      },
      methods: {
      }
    }).mount("#app")
  </script>

定义表单数据:

data: function () {
                return {
                    bookList: [
                        {
                            bname: '《算法导论》',
                            publish: '2006-9',
                            price: 85.00,
                            count: 1
                        },
                        {
                            bname: '《UNIX编程艺术》',
                            publish: '2006-2',
                            price: 59.00,
                            count: 1
                        },
                        {
                            bname: '《编程珠玑》',
                            publish: '2008-10',
                            price: 39.00,
                            count: 1
                        },
                        {
                            bname: '《代码大全》',
                            publish: '2006-3',
                            price: 128.00,
                            count: 1
                        },
                    ]
                }
            },

表单数据输入到表单内:且给购买数量减少操作绑定一个函数decrement,购买数量增加操作绑定一个函数increment,书本移除操作绑定一个函数del。

<tr v-for="(book,index) in bookList" :key="index">
                <td>{{index+1}}</td>
                <td>{{book.bname}}</td>
                <td>{{book.publish}}</td>
                <td>¥{{book.price}}</td>
                <td>
                    <button @click="decrement(index)" :disabled="book.count == 1">- 
                    </button>
                    {{book.count}}
                    <button @click="increment(index)">+</button>
                </td>
                <td>
                    <button @click="del(index)">移除</button>
                </td>
            </tr>

实现对书本操作减少,增加以及删除函数:

methods: {
                decrement(i) {
                    if (this.bookList[i].count > 1) {
                        this.bookList[i].count--
                    }
                },
                increment(i) {
                    this.bookList[i].count++
                },
                del(i) {
                    this.bookList.splice(i, 1)
                }
            }

计算所有书本总价的属性:

computed: {
                //计算属性
                sum() {
                    let sum = 0;
                    this.bookList.forEach(book => {
                        sum += book.price * book.count
                    });
                    return sum;
                }
            },

总代码:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车案例</title>
    <script type="text/javascript" src="./js/vue.global.js"></script>
</head>

<body>
    <div id="app" style="width:90%;margin:auto;">
        <table border="1px" width="500px">
            <tr>
                <td></td>
                <td>书籍名称</td>
                <td>出版日期</td>
                <td>价格</td>
                <td>购买数量</td>
                <td>操作</td>
            </tr>
            <tr v-for="(book,index) in bookList" :key="index">
                <td>{{index+1}}</td>
                <td>{{book.bname}}</td>
                <td>{{book.publish}}</td>
                <td>¥{{book.price}}</td>
                <td>
                    <button @click="decrement(index)" :disabled="book.count == 1">-</button>
                    {{book.count}}
                    <button @click="increment(index)">+</button>
                </td>
                <td>
                    <button @click="del(index)">移除</button>
                </td>
            </tr>
        </table>
        <h2>总价:¥{{sum}}</h2>
    </div>

    <script>
        // 1.创建app
        const app = Vue.createApp({
            // data: option api
            data: function () {
                return {
                    bookList: [
                        {
                            bname: '《算法导论》',
                            publish: '2006-9',
                            price: 85.00,
                            count: 1
                        },
                        {
                            bname: '《UNIX编程艺术》',
                            publish: '2006-2',
                            price: 59.00,
                            count: 1
                        },
                        {
                            bname: '《编程珠玑》',
                            publish: '2008-10',
                            price: 39.00,
                            count: 1
                        },
                        {
                            bname: '《代码大全》',
                            publish: '2006-3',
                            price: 128.00,
                            count: 1
                        },
                    ]
                }
            },
            computed: {
                //计算属性
                sum() {
                    let sum = 0;
                    this.bookList.forEach(book => {
                        sum += book.price * book.count
                    });
                    return sum;
                }
            },
            methods: {
                decrement(i) {
                    if (this.bookList[i].count > 1) {
                        this.bookList[i].count--
                    }
                },
                increment(i) {
                    this.bookList[i].count++
                },
                del(i) {
                    this.bookList.splice(i, 1)
                }
            }
        })

        // 2.挂载app
        app.mount("#app")
    </script>
</body>

</html>

<html lang="en">

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值