vue的几种插槽及具体用法

本文介绍了Vue组件中的插槽使用,包括基本插槽的替换原理,具名插槽通过`slot`的`name`属性实现,以及作用域插槽如何将数据传递给外部。欢迎读者提出宝贵意见。
摘要由CSDN通过智能技术生成
插槽
  • 基本用法:组件son里所有的slot都会被替换
<!--son-->
<div>
	<slot></slot>
</div>
<!--father-->
<son>
	替换slot
</son>
  • 具名插槽(通过slot的name属性)
<!--son-->
<div>
	<slot name = "one"></slot>
	<slot name = "two"></slot>
	<slot name = "three"></slot>
</div>
<!--father-->
<son>
	<template v-slot:one>替换name为one的slot</template>
	<template v-slot:two>替换name为two的slot</template>
	<template v-slot:three>替换name为three的slot</template>
</son>

  • 默认插槽(匹配上没有名字的slot)
<!--son-->
<div>
	<slot name = "one"></slot>
	<slot></slot>
	<slot name = "three"></slot>
</div>
<!--father-->
<son>
	<template v-slot:one></template>
	<template></template>  //会匹配上没有名字的slot
	<template v-slot:three></template>
</son>
  • 作用域插槽(在组件内部通过slot关键字将数据源交到外部的插槽中)
<!--son-->
<div>
	<slot :abc="msg"></slot>
</div>
<!--father-->
<son>
	<template v-slot="obj">
		{{obj}}   <!--obj:{"abc":"aabbcc"}-->
	</template>
</son>
欢迎大家批评指正
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值