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

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代码

    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
        }
    })

欢迎大家有问题指出

  • 55
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 51
    评论
由于我是AI语言模型,无法直接实现代码,以下是用Vue框架实现购物车页面的一些指导: 1. 首先,需要创建一个Vue实例。可以通过Vue CLI或手动引入Vue库来创建。 2. 创建一个包含购物车商品信息的数组。每个商品应该包括名称、价格、数量和图片等信息。 3. 在Vue实例中定义一个data属性,将创建的购物车商品数组赋值给它。例如: ``` data: { cartItems: [ { name: '商品1', price: 10, quantity: 2, image: 'image1.jpg' }, { name: '商品2', price: 20, quantity: 1, image: 'image2.jpg' }, { name: '商品3', price: 30, quantity: 3, image: 'image3.jpg' }, ] } ``` 4. 在Vue模板中使用v-for指令循环渲染购物车商品列表。例如: ``` <div v-for="item in cartItems" :key="item.name"> <img :src="item.image"> <h3>{{ item.name }}</h3> <p>单价:{{ item.price }}元</p> <p>数量:{{ item.quantity }}</p> <p>小计:{{ item.price * item.quantity }}元</p> </div> ``` 5. 添加购物车商品数量的增减功能。可以使用v-model指令绑定商品数量的值,然后通过方法实现增减功能。例如: ``` <div v-for="item in cartItems" :key="item.name"> ... <button @click="decreaseQuantity(item)">-</button> <input type="number" v-model="item.quantity"> <button @click="increaseQuantity(item)">+</button> ... </div> methods: { increaseQuantity(item) { item.quantity++; }, decreaseQuantity(item) { if (item.quantity > 1) { item.quantity--; } } } ``` 6. 添加总价和结算功能。可以通过计算属性计算购物车商品的总价,并在模板中显示。然后添加一个结算按钮,点击后触发一个方法,弹出提示框或跳转到结算页面。例如: ``` <div> ... <p>总价:{{ totalPrice }}元</p> <button @click="checkout">结算</button> </div> computed: { totalPrice() { return this.cartItems.reduce((total, item) => { return total + item.price * item.quantity; }, 0); } }, methods: { checkout() { // 弹出提示框或跳转到结算页面 } } ``` 以上是用Vue框架实现购物车页面的基本步骤和代码示例。具体实现可能会因需求和实际情况有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值