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>
相关推荐
滴水未满8 小时前
uniapp的调试和安装
uni-app
2501_9159090610 小时前
设置了 SSL Pinning 与双向 TLS 验证要怎么抓包
网络·网络协议·ios·小程序·uni-app·iphone·ssl
壹号机长10 小时前
vue3+uniapp 今天及未来六天日期的时间段预约选择,时间段预约当前时间之前禁用选择
uni-app
滴水未满14 小时前
uniapp的页面
前端·uni-app
2501_9160074716 小时前
如何查看 iOS 设备系统与硬件信息,iOS系统信息显示工具
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074718 小时前
iOS APP 开发,从项目创建、证书与描述文件配置、安装测试和IPA 上传
android·ios·小程序·https·uni-app·iphone·webview
滴水未满18 小时前
uniapp的工程
前端·uni-app
云上凯歌2 天前
01_AI工具平台项目概述.md
人工智能·python·uni-app
郑州光合科技余经理2 天前
O2O上门预约小程序:全栈解决方案
java·大数据·开发语言·人工智能·小程序·uni-app·php
2501_916008892 天前
在不越狱前提下导出 iOS 应用文件的过程,访问应用沙盒目录,获取真实数据
android·macos·ios·小程序·uni-app·cocoa·iphone