uni-app 简单实现基本购物车

本文介绍了如何在uni-app中实现购物车功能,包括商品数量的加减、计算总金额和选中/未选中状态,以及删除商品的操作。通过自定义组件简化代码复用,并展示了相关效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uni-app实现购物车加减数量,计算金额数量,选中/未选中 删除商品

先看一下完整效果图 如下:
在这里插入图片描述
在这里插入图片描述
利用对数量的加减实现总数,总价
在这里插入图片描述
对商品的全选
在这里插入图片描述
组件内容:

<template>
	<view class="price display-flex align-end">
		<view class="current display-flex text-primary-color font-maxx" :class="isCurrentBold?'font-bold':''">
			<view class="text-primary-color font-mini"></view>
			{
   {
   currentPrice}}
		</view>
		<view class="original display-flex text-else-color" v-if="originalPrice">
			<view class="text-else-color"></view>
			{
   {
   originalPrice}}
		</view>
	</view>
</template>

<script>
	export default {
   
		// props:["currentPrice","originalPrice"],
		props:{
   
			currentPrice:String,
			originalPrice:String,
			isCurrentBold:{
   
				type:Boolean,
				default:false
			}
		},
		data() {
   
			return {
   
				
			};
		}
	}
</script>

<style>
.current{
   
	margin-right: 10upx;
}
</style>

这就是页面上价钱这一块
在这里插入图片描述
不使用组件的话,可以自己写,主要就是看着好看一点,别的页面要用到的话,直接引用就可以

```c
<template>
	<view class="index container">
		<!-- 商品内容 -->
		<!-- <detriment :detriment="detriment"></detriment> -->
		<view class="detriment display-flex justify-between flex-wrap width-100 flex-row">
			<view class="productItem display-flex  width-100" v-for="(item,index) in detriment"  >
				<!-- 单选 -->
				<checkbox-group class="check" @change="changeitem(item)">
					<checkbox value="item" :checked="item.flag"></checkbox>
				</checkbox-group>
				<!-- 左侧图片 -->
				<div class="left">
					<image :src="item.src"></image>
				</div>
				<!-- 右侧商品介绍 -->
				<div class="message display-flex flex-column justify-between">
					<!-- 商品名称 -->
					<view class="bigTitle font-bold font-max">
						{
   {
   item.bigTitle}}
					</view>
					<!-- 商品价格 -->
					<orginPrice :currentPrice="item.currentPrice" :originalPrice="item.originalPrice"></orginPrice>
					<!-- 限购10-->
					<view class="littleTitle font-middle text-else-color">
						{
   {
   item.littleTitle}}
					</view>
					<!-- 商品数量加减 -->
					<view class="shoppingNumber display-flex justify-between">
						<label class="minute" @click=
使用 uni-app 开发一个购物车页面,首先需要了解 uni-app基本结构和组件库。以下是一个简单的步骤概述: 1. **项目初始化**:确保你已经在本地安装了 uni-app 的 CLI 工具,并创建一个新的项目。 ```sh uni create my-shopcart cd my-shopcart ``` 2. **引入所需依赖**:在`pages/cart`文件夹下新建 `index.vue` 文件,这是页面的主要视图文件。引入uni-app的`list`组件用于展示商品列表,以及`button`、`input`等基础组件。 ```html <template> <view class="cart-page"> <view class="header">购物车</view> <list :data="items" row-key="id" item-width="auto"> <!-- 商品列表项 --> <cell :title="item.name" :value="item.price + '元'" /> </list> <view class="cart-actions"> <button @click="addItem">添加到购物车</button> <input type="number" v-model="quantity" placeholder="请输入数量" /> <button @click="checkout">结账</button> </view> </view> </template> <script setup> import { ref } from '@vue/runtime-dom'; const items = ref([]); const quantity = ref(1); function addItem(item) { // 添加商品逻辑... } function checkout() { // 结算逻辑... } </script> <style scoped> .cart-page { /* 样式自定义 */ } .header { /* 标题样式 */ } .cart-actions { /* 行动按钮区域样式 */ } </style> ``` 3. **数据管理**:在`setup`中,我们可以定义数据模型如商品列表`items`,用户选择的商品数量`quantity`,并编写对应的业务逻辑函数。 4. **状态管理**:如果应用较大,可以考虑使用Vuex进行状态管理,将购物车的数据存储起来,便于在整个应用中共享。 5. **样式处理**:通过`.vue`文件中的`style scoped`部分对页面元素进行样式定制。 6. **路由配置**:确保你在项目的`router.json`或相应的配置文件中设置了购物车页面的路径和显示条件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值