组件封装示例代码:

把下面这段循环体里的代码进行封装。假设父组件名为home.vue 子组件名为newsList.vue
封装前:home.vue代码:

<view class="margin-top padding-lr" v-for="item3 in newsList['news'+current2CatId]" :key="item3.id" @click="$go('news/info?id='+item3.id)">
	
	
<view class="dashed-bottom flex padding-bottom-lg">
    <view class="padding-right-sm" v-if="item3.img">
		<image style="width:208rpx;height:130rpx;" mode="aspectFill" :src="item3.img"></image>
	</view>
		<view class="flex-sub ">
			<view class="">
				{{item3.title}}
			</view>
			<view class="margin-top flex align-center text-gray text-sm">
				<view class="cuIcon-time"></view>
				<view class="margin-left-xs">
					{{item3.createTime}}
				</view>
			</view>
		</view>
	</view>
	
</view>

封装后:
home.vue

<view class="margin-top padding-lr" v-for="item3 in newsList['news'+current2CatId]" :key="item3.id" @click="$go('news/info?id='+item3.id)">

	<NewsList :item="item3" />
	
</view>


newsList.vue

<template>
	<div class="dashed-bottom flex padding-bottom-lg">
		<div class="padding-right-sm" v-if="item.img">
			<img style="width:208rpx;height:130rpx;" :src="item.img" />
		</div>
		<div class="flex-sub">
			<div class="text-cut">
				{{ item.title }}
			</div>
			<div class="margin-top flex align-center text-gray text-sm">
				<div class="cuIcon-time"></div>
				<div class="margin-left-xs">
					{{ item.createTime }}
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		props: {
			item: {
				type: Object,
				required: true
			}
		}
	}
</script>

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值