vue3 slot 插槽 穿透传递

文章介绍了一种在Vue中对组件进行二次封装的方法,特别是如何处理父组件传递的slot。通过在子组件内动态注册slot,可以避免手动操作,提高效率,减少错误。示例中使用了el-image组件,但该方法适用于任何支持slot的组件库。
摘要由CSDN通过智能技术生成

适用于二次封装组件

页面

<template>
	<myComponents>
		<template #error="slotProps">
        	你 slot 的占位内容
      	</template>
	</myComponents>
</template>

myComponents 组件

el-image为被二次封装的组件,可根据实际更换为 vant iview uview 等组件库组件,或者为其他组件

<template>
	<el-image>
		<!-- 循环注册 父级传递下来的slot -->
		<template v-for="(value, key) in $slots" #[key]="slotProps" :key="key">
	      <slot :name="key" v-bind="slotProps"></slot>
	    </template>
	</el-image>
</template>

优点

  1. 根据父传递的slot 进行注册,避免slot传递错误
  2. 节省手动操作时间,空间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值