温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue如何实现简单的购物车功能

发布时间:2022-07-04 09:31:36 来源:亿速云 阅读:197 作者:iii 栏目:开发技术

这篇文章主要介绍“vue如何实现简单的购物车功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue如何实现简单的购物车功能”文章能帮助大家解决问题。

1.实现效果:

vue如何实现简单的购物车功能

2.涉及到的知识点:

toFixed函数、过滤器、reduce高阶函数、v-bind:disabled、v-if

3.代码:

<!DOCTYPE html>
<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>
    <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="(item,index) in books">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.date}}</td>
                    <td>¥{{item.price | finalPrice}}</td>
                    <td>
                        <button @click="item.count--" :disabled="item.count <=1">-</button>
                        {{item.count}}
                        <button @click="item.count++">+</button>
                    </td>
                    <td><button @click="btndelete(index)">移除</button></td>
                </tr>
            </tbody>
        </table>
        <h3>总价格:{{sumPrice | finalPrice}}</h3>
    </div>
    <div v-else><h3>购物车为空</h3></div>
    </div>
    <script src="../../js/vue.js"></script>
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                books: [
                    {
                        id: 1,
                        name: '《算法导论》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 2,
                        name: '《算法导论》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 3,
                        name: '《算法导论》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 4,
                        name: '《算法导论》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 5,
                        name: '《算法导论》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    }
 
                ]
            },
            methods: {
                btndelete(index){
                    this.books.splice(index,1);
                }
            },
            filters: {
                finalPrice(price){
                    return '¥' + price.toFixed(2);
                }
            },
            computed: {
                sumPrice(){
                    // 计算价格法1:
                    // let sum = 0;
                    // for(let book of this.books) {
                    //     sum += book.price * book.count;
                    // }
                    // return  sum;
                    // 计算价格法2,使用reduce函数。
                    return this.books.reduce(((preValue,book)=>preValue + book.count * book.price),0);
                }
            }
        })
    </script>
</body>
</html>

关于“vue如何实现简单的购物车功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI