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>
相关推荐
2501_916013742 分钟前
iOS 上架 App 全流程实战,应用打包、ipa 上传、App Store 审核与工具组合最佳实践
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063211 分钟前
iOS 26 能耗监测全景,Adaptive Power、新电池视图
android·macos·ios·小程序·uni-app·cocoa·iphone
BumBle3 小时前
uniapp 用css实现圆形进度条组件
前端·vue.js·uni-app
iOS阿玮10 小时前
分享一个4.3(a)瓜,一个操作毁了公司3个月的成果。
uni-app·app·apple
00后程序员张11 小时前
苹果软件混淆的工程逻辑,从符号空间到资源扰动的体系化实现
android·ios·小程序·https·uni-app·iphone·webview
街尾杂货店&16 小时前
word属性解释
uni-app
笨笨狗吞噬者1 天前
【uniapp】小程序体积优化,JSON文件压缩
前端·微信小程序·uni-app
源码集结号1 天前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
木易 士心1 天前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
2501_915106322 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone