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>
相关推荐
大佐不会说日语~13 分钟前
Android 16 下 uni-app APP 提示“网络连接失败”的排障与修复
android·uni-app
2501_915106322 小时前
苹果App Store上架费用及流程全面解析
android·ios·小程序·https·uni-app·iphone·webview
白远山5 小时前
家政服务派单平台搭建实战指南:从需求分析到系统设计全流程解析
java·开发语言·架构·uni-app·需求分析
宸翰8 小时前
解决uni-app 中 SVG 图片在 iOS 端显示模糊问题
前端·uni-app
走到天涯海角9 小时前
uniApp蓝牙在手机上测试
智能手机·uni-app
bug总结1 天前
uniapp的easycom自动组件注册机制
uni-app
HF_code1 天前
uniapp-history路由深度指南
uni-app
suliqiang1 天前
【前端技术】 Web 前端技术36年 演进全景图
信息可视化·微信小程序·小程序·前端框架·uni-app·人机交互·xcode
梦曦i2 天前
uni-router v0.2.0重磅发布:全链路拦截+重复导航优化
前端·uni-app
EatFan2 天前
【实战经验】uni-app使用 SSE 踩坑,EventSource不支持怎么办?
android·后端·ios·uni-app