vue购物车效果

一、介绍

vue购物车效果实现了全选、加减和总价等效果

 二、过程

首先写一个表格,第一行写序号、名称等、后面通过v-for遍历data里面的list渲染页面,最后写总价,每一个商品后面跟的单独的总价是当前商品的单价乘以当前商品的数量:

<table id="app" border="1" cellspacing="0" cellpadding="0" width="600" height="500">
			<tr align="center">
				<th><input type="checkbox" v-model="check" @change="checkedchange" /></th>
				<th>序号</th>
				<th>名称</th>
				<th>个数</th>
				<th>单价</th>
				<th>库存</th>
				<th>总价</th>
			<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的使用Vue.js制作购物车效果的示例: 首先,创建一个Vue实例: ``` var app = new Vue({ el: '#app', data: { products: [ { id: 1, name: 'iPhone X', price: 999.99, quantity: 0 }, { id: 2, name: 'iPad Pro', price: 799.99, quantity: 0 }, { id: 3, name: 'MacBook Pro', price: 1499.99, quantity: 0 } ], cart: [] }, methods: { addToCart: function(product) { if(product.quantity > 0) { var index = this.cart.findIndex(item => item.id === product.id); if(index >= 0) { this.cart[index].quantity += product.quantity; } else { this.cart.push({ id: product.id, name: product.name, price: product.price, quantity: product.quantity }); } product.quantity = 0; } }, removeFromCart: function(product) { var index = this.cart.findIndex(item => item.id === product.id); if(index >= 0) { this.products.find(item => item.id === product.id).quantity += this.cart[index].quantity; this.cart.splice(index, 1); } }, getTotal: function() { var total = 0; this.cart.forEach(item => { total += item.price * item.quantity; }); return total.toFixed(2); } } }); ``` 在这里,我们定义了一个包含商品和购物车的数据对象。商品包括ID、名称、价格和数量属性,而购物车仅包括ID、名称、价格和数量属性。我们还定义了三个方法: - addToCart:将商品添加到购物车中,然后将其数量重置为0。 - removeFromCart:从购物车中删除商品,并将其数量添加回商品列表中。 - getTotal:计算购物车中所有商品的总价值。 接下来,我们可以在Vue模板中使用这些数据和方法来创建购物车效果: ``` <div id="app"> <div class="product-list"> <div v-for="product in products" :key="product.id"> <h3>{{ product.name }}</h3> <p>{{ product.price | currency }}</p> <input type="number" v-model.number="product.quantity" min="0" max="10"> <button @click="addToCart(product)">Add to Cart</button> </div> </div> <div class="cart"> <h2>Shopping Cart</h2> <div v-for="product in cart" :key="product.id"> <h3>{{ product.name }}</h3> <p>{{ product.price | currency }}</p> <input type="number" v-model.number="product.quantity" min="0" max="10"> <button @click="removeFromCart(product)">Remove from Cart</button> </div> <p>Total: {{ getTotal() | currency }}</p> </div> </div> ``` 在这里,我们使用v-for指令来遍历商品列表购物车,并使用v-model指令来绑定商品数量输入框和购物车数量输入框。我们还使用过滤器来格式化价格和总价值。最后,我们使用@click指令来触发添加和删除商品的事件。 这是一个简单的购物车示例,你可以根据自己的需要进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值