使用Vue实现购物车功能

要实现的效果如图下

遍历循环data中购物车的数据渲染到页面上,产品的所有字段,购买的数量,以及单价

先遍历直接能用的数据:

接着对购物车产品数量表单进行双向数据绑定,给左右按钮加一个点击事件使用三元表达式判断当前产品数量进行累加或者递减

通过单价*数量绑定总金额:

接下来给产品列表,每一项添加一个select字段,判断当前产品是否被选中,默认应该都是选中的:

接下来在计算属性中添加一个获取订单总金额的方法:

接下来单选按钮添加点击事件,对是否选中进行取反操作,并给选中按钮的绑定样式:

看一眼效果基本实现了

 接下来实现全选的按钮:

添加一个产品选择方法

绑定到全选按钮的点击事件并把是否全选作为参数传递进去

到这里全选按钮也完工了

接下来删除单条产品,通过索引可以获取到当前的条目:

在methods里定义删除单条方法:

绑定点击事件

在method添加一个删除选中的事件

 

 到这里项目就完工了

http://delisteam.gitee.io/vuegouwuchelianxi/

 

转载于:https://www.cnblogs.com/rmty/p/11056916.html

实现购物车功能,可以按以下步骤进行: 1. 创建一个包含商品信息的数组,其中每个商品对象包含id、名称、价格、数量等属性。 2. 在Vue使用v-for指令遍历数组中的商品对象,并使用flex布局将商品横向排列。 3. 在购物车页面,可以使用computed属性计算出购物车中所有商品的总价和总数量。 4. 在每个商品对象中添加一个购物车状态的属性,例如isInCart,初始为false。点击加入购物车按钮,将该属性设置为true,并将商品对象添加到购物车数组中;点击移出购物车按钮,将该属性设置为false,并从购物车数组中移除该商品对象。 5. 在v-for指令中添加一个v-if条件,判断商品的数量是否为0,若为0则不显示该商品。 下面是一个示例代码: HTML: ```html <div id="app"> <div class="product-list"> <div v-for="product in productList" :key="product.id" class="product-card"> <img :src="product.image" alt=""> <div class="info"> <h3>{{ product.name }}</h3> <p>{{ product.description }}</p> <div class="price">{{ product.price }}</div> <div class="quantity"> <button @click="decreaseQuantity(product)">-</button> <span>{{ product.quantity }}</span> <button @click="increaseQuantity(product)">+</button> </div> <button @click="addToCart(product)" v-if="!product.isInCart">加入购物车</button> <button @click="removeFromCart(product)" v-else>移出购物车</button> </div> </div> </div> <div class="cart"> <h2>购物车</h2> <div v-for="product in cart" :key="product.id" class="cart-item"> <div class="cart-info"> <h3>{{ product.name }}</h3> <div class="cart-price">{{ product.price }}</div> <div class="cart-quantity">{{ product.quantity }}</div> </div> </div> <div class="cart-total"> <div>总价: {{ totalPrice }}</div> <div>总数量: {{ totalQuantity }}</div> </div> </div> </div> ``` CSS: ```css .product-list { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; } .product-card { width: 300px; margin-bottom: 20px; border: 1px solid #ddd; padding: 20px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); } .product-card img { width: 100%; } .info { display: flex; flex-direction: column; justify-content: space-between; height: 100%; } .price { font-size: 24px; font-weight: bold; } .quantity { display: flex; align-items: center; } .cart { margin-top: 50px; } .cart-item { display: flex; align-items: center; margin-bottom: 20px; border: 1px solid #ddd; padding: 20px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); } .cart-info { display: flex; flex-direction: column; justify-content: space-between; height: 100%; } .cart-price { font-size: 24px; font-weight: bold; } .cart-quantity { font-size: 18px; font-weight: bold; } ``` JS: ```js new Vue({ el: '#app', data: { productList: [ { id: 1, name: '商品1', description: '这是商品1的描述', price: 100, image: 'https://via.placeholder.com/300x200', quantity: 0, isInCart: false, }, { id: 2, name: '商品2', description: '这是商品2的描述', price: 200, image: 'https://via.placeholder.com/300x200', quantity: 0, isInCart: false, }, { id: 3, name: '商品3', description: '这是商品3的描述', price: 300, image: 'https://via.placeholder.com/300x200', quantity: 0, isInCart: false, }, ], cart: [], }, computed: { totalPrice() { return this.cart.reduce((total, product) => total + product.price * product.quantity, 0); }, totalQuantity() { return this.cart.reduce((total, product) => total + product.quantity, 0); }, }, methods: { increaseQuantity(product) { product.quantity += 1; }, decreaseQuantity(product) { if (product.quantity > 0) { product.quantity -= 1; } }, addToCart(product) { product.isInCart = true; this.cart.push(product); }, removeFromCart(product) { product.isInCart = false; const index = this.cart.indexOf(product); this.cart.splice(index, 1); }, }, }); ``` 在这个示例中,我们使用了一个productList数组来存储所有的商品信息,使用v-for指令遍历数组中的商品对象,并使用flex布局将商品横向排列。每个商品对象都有一个quantity属性来记录该商品的数量,以及一个isInCart属性来记录该商品是否在购物车中。点击加入购物车按钮,将该商品的isInCart属性设置为true,并将该商品对象添加到cart数组中;点击移出购物车按钮,将该商品的isInCart属性设置为false,并从cart数组中移除该商品对象。在v-for指令中添加一个v-if条件,判断商品的数量是否为0,若为0则不显示该商品。在购物车页面中,使用computed属性计算出购物车中所有商品的总价和总数量,并显示在页面中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值