Vue3实现pc端商城购物车Demo

主要包含全选,店铺全选,反选,商品数量,价格计算
最终效果如下
在这里插入图片描述

数量加减使用的elementplus 中el-input-number组件

<template>
	<div>
		<div class="goodsCart">
			<div class="container">
				<div class="tit">
					全部商品
				</div>
				<div id="tree">
					<div class="tree">
						<div class="tree-title">
							<ul>
								<li class="width-6">
									<input type="checkbox" ref="businessInp" @click="selectAll" v-model="allDataCheck" />
									全部
								</li>
								<li class="width-48">商品信息</li>
								<li class="width-10">单价()</li>
								<li class="width-10">数量</li>
								<li class="width-16">金额</li>
								<li class="width-10">操作</li>
							</ul>
						</div>

						<div class="tree-merchant" v-for="(item, index) of data" :key="index">
							<div class="tree-merchant-name">
								<input type="checkbox" v-model="business" :value="item.id" @click="selectBusiness($event,item.id,item.goods)" />
								<div class="name">{
   {
   item.name}}</div>
							</div>

							<div class="tree-goods" v-for="(goods, indexs) of item.goods" :key="indexs">
								<ul>
									<li class="width-54">
										<input type="checkbox" v-model="goodsInp" :value="goods.id" @click="singleChoice($event,goods.id,item.goods,item.id)" />
										<img src="">
										<div class="goods-name">
											<div>{
   {
   goods.name}}</div>
											<div class="product-spec">
												<span>颜色:夏日琥珀</span><span>RAM容量:8GROM</span><span>容量:256G</span></div>
										</div>
									</li>
									<li class="width-10">
										<div class="price">
											¥{
   {
   goods.unitPrice}}
										</div>
							 	</li>
									<li class="width-10">
										<div class="num">
											<el-input-number size="mini" :min="1" v-model="goods.num"
												@change="handleChange" />
										</div>
									</li>
									<li class="width-16">
										<div class="price">
											¥{
   {
   goods.unitPrice*goods.num}}
										</div>
  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的Vue3购物车页面示例,你可以根据自己的需求进行修改和优化: ```html <template> <div class="cart"> <table> <thead> <tr> <th>商品</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item, index) in cartList" :key="index"> <td>{{ item.name }}</td> <td>{{ item.price }}</td> <td> <button @click="minus(index)" :disabled="item.count <= 1">-</button> <input type="text" v-model.number="item.count"> <button @click="add(index)">+</button> </td> <td>{{ item.price * item.count }}</td> <td> <button @click="remove(index)">删除</button> </td> </tr> </tbody> <tfoot> <tr> <td colspan="3">合计:</td> <td>{{ totalPrice }}</td> <td> <button @click="clear">清空购物车</button> </td> </tr> </tfoot> </table> </div> </template> <script> export default { data() { return { cartList: [ { name: '商品1', price: 100, count: 1 }, { name: '商品2', price: 200, count: 3 }, { name: '商品3', price: 300, count: 2 }, ], }; }, computed: { totalPrice() { return this.cartList.reduce((total, item) => { return total + item.price * item.count; }, 0); }, }, methods: { add(index) { this.cartList[index].count++; }, minus(index) { this.cartList[index].count--; }, remove(index) { this.cartList.splice(index, 1); }, clear() { this.cartList = []; }, }, }; </script> <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } button { border: none; background-color: #eee; padding: 4px 8px; cursor: pointer; outline: none; &:disabled { opacity: 0.5; cursor: not-allowed; } } tfoot td { font-weight: bold; } </style> ``` 在这个示例中,我们使用了Vue3的新特性`<script setup>`,这个特性可以让我们更轻松地编写组件,避免了一些重复的代码。 这个组件包含一个表格,每行显示一个商品的信息,包括商品名称、单价、数量、小计和操作。我们使用`v-for`指令遍历购物车列表,并使用`v-model`指令实现数量的双向绑定。同时,我们还使用了计算属性来计算购物车的总价。 在方法中,我们实现了添加、减少、删除和清空购物车的功能。其中,添加和减少数量的方法只需要改变对应商品的数量即可,删除方法使用`splice`函数从购物车列表中删除对应的商品,清空购物车方法直接将购物车列表清空即可。 最后,我们还为按钮添加了一些样式,使其更加美观。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值