Vue2 实现购物车功能(可直接使用)

news/2024/7/4 7:26:25 标签: javascript, 开发语言, ecmascript, vue.js

目录

      • vue2.0实例简单购物车
      • 代码实现

vue2.0实例简单购物车

页面展示效果如下:​

在这里插入图片描述
该购物车实现了自动计算小计、总价。

代码实现

<body>
    <div id="app">
        <div>
            <form action="">
                商品名称:<input type="text" v-model="productName" name="productName">
                商品单价:<input type="text" v-model="productPrice" name="productPrice">
                <input type="button" value="添加商品" @click="addProduct">
            </form>
        </div>
        <ul>
            <li v-for="(pro,index) in productList"  :key="index">
                商品名称: {{pro.productName}} ---------------- 商品单价: {{pro.productPrice}} 
                <button @click="addproToCart(index)">添加商品</button>
            </li>
        </ul>
        <cart :cartlist="cartList"></cart>
    </div>

    <template id="cartHtml">
        <div>
            <table border="1">
                <tr>
                    <td>商品</td>
                    <td>商品名称</td>
                    <td>商品价格</td>
                    <td>商品数量</td>
                    <td>商品价格</td>
                </tr>
                <tr v-for="(pro,index) in cartlist" :key="index">
                    <td><input type="checkbox" v-model="pro.active"></td>
                    <td>{{pro.productName}}</td>
                    <td>{{pro.productPrice}}</td>
                    <td>
                        <button @click="reduceProNum(index)">-</button>
                        {{pro.productNum}}
                        <button @click="addProNum(index)">+</button>
                    </td>
                    <td>{{pro.productPrice * pro.productNum}}</td>
                </tr>
                <tr>
                    <td colspan="3">选中的商品:{{activeNum}}/{{cartlist.length}}</td>
                    <td colspan="2">商品总价:{{totalprice}}</td>
                </tr>
            </table>
        </div>
    </template>    
</body>

js代码

javascript">    var cart = {
        template:'#cartHtml',
        props:['cartlist'],
        methods:{
            // 商品数量+1
            addProNum(index){
                let product = this.cartlist[index];
                product.productNum++
            },
            // 商品数量-1
            reduceProNum(index){
                let product = this.cartlist[index];
                // 判断商品数量是否为1
                if(product.productNum==1){
                    this.cartlist.splice(index,1) // 为1,从数组中删除
                }else{
                    product.productNum--
                }
            }
        },
        computed:{
            activeNum(){
                let activeProdctList = this.cartlist.filter(item=>{
                    return item.active
                })
                return activeProdctList.length
            },
            totalprice(){
                let result = 0;
                for(pro of this.cartlist){
                    if(pro.active){
                        result = result + pro.productPrice * pro.productNum
                    }
                }
                return result;
            }
        }
    }

    var app = new Vue({
        el:'#app',
        data(){
            return{
                productName:'',
                productPrice:0,
                productList:[],
                cartList:[]
            }
        },
        methods:{
            addProduct(){
                // todo 对新增的商品名称和单价,进行验证

                // 查找新增的商品是否存在于商品列表中,如果不在存在返回-1
                let findindex = this.productList.findIndex(item=>{
                    return item.productName==this.productName
                })
                if(findindex==-1){ // 判断商品列表中是否存在新增商品
                    // 把新商品添加到商品列表中
                    this.productList.push({productName:this.productName,productPrice:this.productPrice})
                }else{
                    alert('此商品已经存在') // 商品已存在,给出提示
                }
            },
            // 添加商品到购物车,index是单击商品的下标
            addproToCart(index){
                
                let newproduct = this.productList[index]; // 根据下标从商品列表中取出商品对象
                // 从购物车列表中查找,是否存在新的商品,如果查到返回购物车中的商品
                let product = this.cartList.find(item=>{
                    return item.productName==newproduct.productName
                })
                if(product){
                    // 如果有对应商品数量加1
                    product.productNum++
                }else{
                    // 没有对应商品,添加新的商品到购物车
                    this.cartList.push({
                        productName:newproduct.productName,
                        productPrice:newproduct.productPrice,
                        productNum:1,
                        active:true
                    });
                }
                
                console.log(product);
            }
        },
        components:{
            cart
        }
    })

欢迎大家有问题指出


http://www.niftyadmin.cn/n/4937229.html

相关文章

ChatGPT: 提升程序员开发效率的秘密武器!

引言 在现代软件开发中&#xff0c;时间和效率显得尤为重要。程序员们需要在尽可能短的时间内编写高质量的代码&#xff0c;并使之处于状态良好的维护周期。为满足这些需求&#xff0c;人工智能技术逐渐成为软件开发的一项核心能力。ChatGPT作为自然语言生成模型中的佼佼者&am…

关于PMP中的敏捷知识

在此次改版中&#xff0c;题目中增加了大量的敏捷题型&#xff0c;总体比重仍然很高&#xff0c;据我的主观感觉&#xff0c;达到了1/3。与ACP认证相比&#xff0c;PMP对敏捷管理技术的考察相对简单&#xff0c;考察路径也较为集中&#xff0c;主要聚焦于以下两个方面&#xff…

Go和Java实现中介者模式

Go和Java实现中介者模式 下面通过一个同事之间相互通信的例子来说明中介者模式的使用。 1、中介者模式 中介者模式是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类&#xff0c;该类通常处理不同类之间的 通信&#xff0c;并支持松耦合&#xff0c;使代码…

VMware Workstation 如何启用复制粘贴

产品&#xff1a;VMware Workstation 16 Pro 版本&#xff1a;16.1.1 build-17801498 我们刚安装好的 VMware Workstation 会发现无法复制粘贴文件到虚拟机中&#xff0c;如下为解决方案&#xff1a; 1.点击 虚拟机&#xff0c;点击 安装 VMware Tools(T)...。 2.虚拟机下面会…

机器学习/深度学习需要掌握的linux基础命令

很多深度学习/机器学习/数据分析等领域&#xff08;或者说大多数在Python环境下进行操作的领域&#xff09;的初学者入门时是在Windows上进行学习&#xff0c;也得益于如Anaconda等工具把环境管理做的如此友善 但如果想在该领域继续深耕&#xff0c;一定会与Linux操作系统打交…

torch.profiler

什么是torch.profiler PyTorch Profiler 是一个工具&#xff0c;它允许在训练和推理期间收集性能指标。Profiler 的上下文管理器 API 可用于更好地了解哪些模型操作最昂贵&#xff0c;检查它们的输入形状和调用堆栈&#xff0c;研究设备内核活动并可视化执行跟踪。 性能指标&…

Springboot 设置统一的请求返回格式

现在开发过程中主要采用前后端分离的方式进行开发测试&#xff0c;也就是前端封装请求&#xff0c;后端提供标准的API接口服务。一般现在json 格式受到开发者们的青睐&#xff0c;学习过程中我们可以设置接口的返回类型&#xff0c;那么怎么做到设置统一的返回格式呢&#xff1…

数据结构顺序表和链表(超详细)

线性表&#xff1a; 线性表 &#xff08; linear list &#xff09; 是 n 个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使 用的数据结构&#xff0c;常见的线性表&#xff1a;顺序表、链表、栈、队列、字符串 ... 线性表在逻辑上是线性结构&#xff0c;也就…