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>
相关推荐
爱喝水的小周17 分钟前
《UniApp 页面配置文件pages.json》
前端·uni-app·json
iOS阿玮3 小时前
别问了,我自己的产品也卡审了44个小时!
uni-app·app·apple
2501_915918413 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
用户97141718142711 小时前
UniApp + Vue3 持久化登录(清除后台仍保持登陆状态)
uni-app
小白学鸿蒙12 小时前
新手记录使用uniapp-x开发鸿蒙应用
华为·uni-app·harmonyos
初遇你时动了情1 天前
uniapp/flutter中实现苹果IOS 26 毛玻璃效果、跟随滑动放大动画
flutter·ios·uni-app
gys98951 天前
uniapp使用sqlite模块
数据库·sqlite·uni-app
abigale031 天前
开发实战 - ego商城 -补充:使用uniapp扩展组件
uni-app·uni-ui
2501_916007471 天前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
爱喝水的小周1 天前
《UniApp 页面导航跳转全解笔记》
前端·uni-app