uniapp中插槽的使用

首先,简单使用
子组件

<template>
	<view class="">
		<slot>发布</slot>
	</view>
</template>

父组件

<template>
	<view class="release" :style="{'margin-top':statusBarHeight+'rpx'}">
		<Queation>111</Queation>
		<!-- 如果子组件标签内什么都不写,则会显示默认的“发布”两个字-->
	</view>
</template>

<script>
	import Queation from '@/components/common/question.vue'
	export default {
		data() {
			return {
			}
		},
		components:{
			Queation
		},
	}
</script>

结果:
在这里插入图片描述
具名插槽:需要多个插槽时,可以利用 元素的一个特殊的特性:name 来定义具名插槽
子组件

<template>
	<view>
		<slot name="button">发布</slot>
		<br/>
		<slot name="content"></slot>
	</view>
</template>

父组件:必须在 元素上使用 v-slot 指令,并以 v-slot 的参数的形式提供其名称

<template>
	<view >
		<Queation>
			<template v-slot:button>
				这是name为button的插槽
			</template>
			<template v-slot:content>
				这是name为content的插槽
			</template>
		</Queation>
	</view>
</template>

<script>
	import Queation from '@/components/common/question.vue'
	export default {
		data() {
			return {
			}
		},
		components:{
			Queation
		},
	}
</script>

结果:
在这里插入图片描述
具名插槽的缩写: 跟 v-on 和 v-bind 一样,v-slot 也有缩写,即把参数之前的所有内容 (v-slot:) 替换为字符 #。例如 v-slot:header 可以被重写为 #header :

<template>
	<view>
		<Queation>
			<template #button>
				这是name为button的插槽
			</template>
			<template #content>
				这是name为content的插槽
			</template>
		</Queation>
		
	</view>
</template>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值