在uniapp开发微信小程序时,v-for中使用slot的正确姿势

最新情况(截至2024-03)

经过确认,HBuilderX 3.8.12+,已经可以不用按照本文内容进行手动处理 generic:scoped-slots-itemView="my-component"这个操作了。大家可以放心使用。此版本编译后的wxml文件,已经自动生成了上述内容。

经过验证,本文中所述方法:

<template>
	<view>
		。。其他内容。。。。
		<CommonList>
			<MyComponent slot="itemView" slot-scope="{item}" :item="item"></MyComponent> 
		</CommonList>
	</view>
</template>

......
</script>

或者

<template>
	<view>
		<CommonList>
			<view slot="itemView" slot-scope="{item}">
			    只显示ID:{
  
  {item.id}}
			</view> 
		</CommonList>
	</view>
</template>

......
</script>

都是能正确编译成generic:scoped-slots-itemView这种形式。

PS:为什么用Vue2的老式写法,因为我发现用v-slot或者Vue3的写法,都会有各种灵异问题,这里就不赘述了,请大家自行考虑怎么用吧。

背景

众所周知,大部分使用uniapp开发微信小程序的同学,一定会遇到使用vue开开心心写一个v-for,然后v-for中搞一个slot,结果在使用的时候,H5没问题,微信小程序就只渲染第一个的问题。

比如我有一个组件,假设叫CommonList
这个组件里面有一个v-forv-for中有一个slot,它循环渲染处理products

<template>
	。。其他内容。。。。
	<view class="lists">
		<view class="data-item" v-for="(item, index) in products" :key="index">
			<text>标题</text>
			<slot name="itemView" :item="item">
				<view>
					<text>{
  
  {item}}</text
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sb熙哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值