Vue模拟实现购物车结算功能

 更新时间:2022年04月13日 14:57:40   作者:池鱼i_  
这篇文章主要为大家详细介绍了Vue模拟实现购物车结算功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Vue实现购物车结算功能的具体代码,供大家参考,具体内容如下

<!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>Document</title>
    <script src="js/vue.js" type="text/javascript"></script>
    <script src="js/jquery-3.6.0.js" type="text/javascript"></script>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0
        }
        
        a {
            text-decoration: none;
        }
        .container {
            width: 500px;
            margin: 10px auto;
        }
        
        .title {
            width: 500px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            font-size: 20px;
            background-color: paleturquoise;
        }
        
        .item {
            position: relative;
            height: 50px;
            border-bottom: 1px solid paleturquoise;
        }
        
        .item img {
            float: left;
            width: 100px;
            height: 50px;
        }
        
        .item .price {
            position: absolute;
            float: left;
            width: 120px;
            margin-left: 10px;
            top: 15px;
            left: 100px;
        }
        
        .item .change {
            position: absolute;
            left: 220px;
            top: 15px;
            float: left;
            width: 200px;
        }
        
        .change a {
            float: left;
            display: block;
            width: 20px;
            height: 20px;
            font-size: 18px;
            text-align: center;
            line-height: 20px;
            background-color: #ccc;
        }
        
        .change input {
            float: left;
            width: 50px;
            margin: 0 5px;
        }
        
        .item .del {
            position: absolute;
            top: 8px;
            left: 420px;
            color: red;
            font-size: 24px;
        }
        
        .item .del:hover {
            top: 0;
            height: 50px;
            background-color: blue;
        }
        
        .total {
            position: relative;
            width: 500px;
            height: 50px;
            background-color: cornflowerblue;
        }
        
        .total span {
            position: absolute;
            top: 14px;
            left: 250px;
        }
        
        .total span em {
            color: red;
            font-style: normal;
            font-size: 20px;
        }
        
        .total button {
            position: absolute;
            top: 8px;
            left: 400px;
            width: 50px;
            height: 35px;
            border-radius: 25%;
            border: none;
            outline: none;
            background-color: tomato;
        }
    </style>
 
</head>
<body>
    <div id="app">
 
  <div>
    <div class="container">
        <my-cart></my-cart>
    </div>
  </div>
  
    </div>
    <script type="text/javascript">
   // 三个子组件
   var CartTitle = {
            props: ['uname'],
            template: `<div class="title">{{uname}}的商品</div>`
        }
        var CartList = {
            props: ['list'],
            template: ` <div class="list">
                    <div class="item" :key="item.id" v-for="item in list">
                        <img :src="item.img" alt="">
                        <div class="price">{{item.price}}¥/件</div>
                        <div class="change">
                            <a href="" @click.prevent=" sub(item.id)">-</a>
                            <input type="text" class="num" :value="item.num" @blur="changenum(item.id,$event)">
                            <a href="" @click.prevent=" add(item.id)">+</a>
                        </div>
                        <div class="del" @click="del(item.id)">×</div>
                    </div>
                </div>
                    `,
            methods: {
                // 向父组件传递需要删除的id
                del: function(id) {
                    // console.log(id);
                    this.$emit("del-cart", id);
                },
                // 修改表单输入的数量
                changenum: function(id, event) {
                    //console.log(id, event.target.value);
                    // 向父组件传递然后再修改数量
                    this.$emit('change-num', {
                        id: id,
                        num: event.target.value
                    })
                },
                // 点击减号按钮
                sub: function(id) {
                    this.$emit('sub-num', id);
                },
                //点击加号按钮
                add: function(id) {
                    this.$emit('add-num', id);
                }
            }
        }
        var CartTotal = {
                props: ['list'],
                template: `<div class="total">
                    <span>总价:<em>{{total}}</em>¥</span>
                    <button>结算</button>
                </div>`,
                computed: {
                    total: function() {
                        var sum = 0;
                        this.list.forEach(item => {
                            sum += item.price * item.num;
                        });
                        return sum;
                    }
                }
            }
            // 定义父组件
        Vue.component('my-cart', {
            data: function() {
                return {
                    uname: '我',
                    list: [{
                        id: 1,
                        name: '安踏鞋子',
                        price: 260,
                        num: 1,
                        img: 'img/a.jpg'
                    }, {
                        id: 2,
                        name: '海尔热水器',
                        price: 2000,
                        num: 1,
                        img: 'img/hai.jpg'
                    }, {
                        id: 3,
                        name: 'iphone手机',
                        price: 7000,
                        num: 1,
                        img: 'img/iphone.jpg'
                    }, {
                        id: 4,
                        name: '华为手机',
                        price: 4500,
                        num: 1,
                        img: 'img/h.jpg'
                    }]
                }
            },
            template: `<div class="mycart">
                <cart-title :uname="uname"></cart-title>
                <cart-list :list="list" @del-cart="delcart($event)" @change-num="changeNum($event)" @sub-num="subnum($event)" @add-num="addnum($event)"></cart-list>
                <cart-total :list="list"></cart-total>
                </div>`,
            components: {
                'cart-title': CartTitle,
                'cart-list': CartList,
                'cart-total': CartTotal,
            },
            methods: {
                delcart: function(id) {
                    // 根据id删除list中对应的数据
                    // 1.找到id对应数据的索引
                    var index = this.list.findIndex(item => {
                        return item.id == id;
                    });
                    // 2.根据索引删除对应的数据
                    this.list.splice(index, 1);
                },
                // 根据id修改list中的数量num
                changeNum: function(val) {
                    //console.log(val);
                    this.list.some(item => {
                        if (item.id == val.id) {
                            item.num = val.num;
                        }
                    })
                },
                //减号减少num
                subnum: function(event) {
                    // console.log(event); event是点击的id号
                    this.list.some(item => {
                        if (item.id == event && item.num > 0) {
                            item.num--;
                        }
                    })
                },
                // 加号增加num
                addnum: function(event) {
                    this.list.some(item => {
                        if (item.id == event) {
                            item.num++;
                        }
                    })
                }
            }
        });
        var vm = new Vue({
            el: "#app",
            data: {
 
            }
        });
 
    
    </script>
    
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue router导航守卫(router.beforeEach())的使用详解

    vue router导航守卫(router.beforeEach())的使用详解

    导航守卫主要用来通过跳转或取消的方式守卫导航。这篇文章主要介绍了vue-router导航守卫(router.beforeEach())的使用,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • vue 进阶之实现父子组件间的传值

    vue 进阶之实现父子组件间的传值

    这篇文章主要介绍了vue 进阶之实现父子组件间的传值,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • Vue事件处理中的上下文问题:原因与解决过程

    Vue事件处理中的上下文问题:原因与解决过程

    本文将探讨 Vue 事件处理中常见的上下文问题及其解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Vue.set() this.$set()引发的视图更新思考及注意事项

    Vue.set() this.$set()引发的视图更新思考及注意事项

    this.$set()和Vue.set()本质方法一样,前者可以用在methods中使用。这篇文章主要介绍了Vue.set() this.$set()引发的视图更新思考及注意事项,需要的朋友可以参考下
    2018-08-08
  • vue去除数组指定位置元素的几种方法

    vue去除数组指定位置元素的几种方法

    这篇文章主要介绍了vue剔除数组指定位置元素的几种方法,文中主要介绍了单个去除和批量去除这两种方法,并通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-03-03
  • vue在 for 循环里使用异步调用 async/await的方法

    vue在 for 循环里使用异步调用 async/await的方法

    大家都遇到这样的问题,在使用函数的async/await异步调用时候,放在正常函数中单个调用时没有问题的,但是await放在forEach()循环里面就会报错,本文给大家介绍vue 如何在 for 循环里面使用异步调用 async/await,感兴趣的朋友一起看看吧
    2023-10-10
  • Vue表单控件绑定图文详解

    Vue表单控件绑定图文详解

    在本文中我们给大家整理了一篇关于Vue表单控件绑定的相关知识点内容,有需要的朋友们参考下。
    2019-02-02
  • Vue3多组件的N种编写方式

    Vue3多组件的N种编写方式

    Vue 本身以及周边生态在设计语法糖上几乎没让我失望过,包括本次亮相的 Vue Vine,它的出现引起了我对 Vue3 组件编写方式的好奇,以及哪一种方式更接近「最佳实践」?下面让我来为大家一一道来
    2024-07-07
  • vite+vue3项目初始化搭建的实现步骤

    vite+vue3项目初始化搭建的实现步骤

    本文主要介绍了vite+vue3项目初始化搭建的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • Avue 组件库的使用初体验

    Avue 组件库的使用初体验

    这篇文章主要为大家介绍了Avue 组件库的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08

最新评论