uniapp:封装商品列表为组件并使用

封装商品列表为组件并使用

商品组件封装

html 复制代码
<template>
	<!-- 商品列表 -->
	<view class="goods_list">
		<view class="goods_item" v-for="item in goods" :key="item.id">
			<image :src="item.img_url"></image>
			<view class="price">
				<text>¥{{item.sell_price}}</text>
				<text>¥{{item.market_price}}</text>
			</view>
			<view class="name">
				{{item.title}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: ['goods'], // 接收父组件传值
		name: "goods-list",
		data() {
			return {

			};
		}
	}
</script>

<style lang="scss">
	.goods_list {
		padding: 0 15rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		.goods_item {
			background: #fff;
			width: 355rpx;
			margin: 10rpx 0;
			padding: 15rpx;
			box-sizing: border-box;

			image {
				width: 80%;
				height: 150px;
				display: block;
				margin: auto;
			}

			.price {
				color: $shop-color;
				font-size: 36rpx;
				margin: 20rpx 0 5rpx 0;

				text:nth-child(2) {
					color: #ccc;
					font-size: 28rpx;
					margin-left: 17rpx;
					text-decoration: line-through;
				}
			}

			.name {
				font-size: 28rpx;
				line-height: 50rpx;
				padding-bottom: 15rpx;
				padding-top: 10rpx;
			}
		}
	}
</style>

使用组件示例

html 复制代码
<template>
	<view>
		<goods-list :goods="goods"></goods-list> 
	</view>
</template>

<script>
	import goodsList from "../../components/goods-list/goods-list.vue"//1.导入
	export default {
		data() {
			return {
				goods: [],
				pageIndex: 1
			}
		},
		onLoad() {
			this.getHotGoods();
		},
		methods: {
			// 获取热门商品列表数据
			async getHotGoods() {
				const res = await this.$myRequest({
					url: '/api/getgoods?pageindex=' + this.pageIndex
				})
				this.goods = res.data.message
			}
		},
		components: {
			"goods-list": goodsList //2.注册
		}
	}
</script>
相关推荐
小恒恒7 小时前
2025 Vibe Coding 有感
前端·uni-app·trae
一颗小青松8 小时前
uniapp使用uni-im
uni-app
2501_9160074711 小时前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063211 小时前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
一颗小青松14 小时前
uniapp vue3中app端使用腾讯云点播上传
uni-app·云计算·腾讯云
玄尺_00715 小时前
uniapp h5端使浏览器弹出下载框
前端·javascript·uni-app
2501_9151063216 小时前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
web前端神器1 天前
vue、uniapp项目循环中能显示每个列表的内容,但是点击的时候传递的参数却不正确
uni-app
ModyQyW1 天前
HBuilderX 4.87 无法正常读取 macOS 环境配置的解决方案
前端·uni-app
脾气有点小暴1 天前
Uni-app App 端自定义导航栏完整实现指南
uni-app