插槽的定义:

和Vue一样,是实现一套内容分发的 API,实现组件内容的分发

默认插槽:

和具名插槽不同,组件内任意内容都可插入

index.vue:

<template>
	<view class="content">
		<slot1></slot1>
	</view>
</template>
<script>
	import slot1 from "./slot.vue"
	export default {
		components:{
			slot1
		},
	}
</script>
<style>
</style>

有一个slot1组件,此时slot1的内容为:
slot.vue:

<template>
	<view>
		111
	</view>
</template>
<script>
</script>
<style>
</style>

此时界面为
在这里插入图片描述
当我们给组件加插槽,给vue加一点内容时,代码就变成:

<template>
	<view class="content">
		<slot1>
			123
		</slot1>
	</view>
</template>
<template>
	<view>
		111
		<slot></slot>
	</view>
</template>

此界面就变成:
在这里插入图片描述

具名插槽

如果把以上的组件代码修改为带name属性

<template>
	<view>
		111
		<slot name="zs"></slot>
	</view>
</template>

此时,123就会消失。
想要让123再次出现就要把index.vue修改为:

<template>
	<view class="content">
		<slot1>
			<text slot="zs">123</text>
		</slot1>
	</view>
</template>

这是由于具名插槽只会把具有相同的名字的代码块插入相应的位置。

Logo

Vue社区为您提供最前沿的新闻资讯和知识内容

更多推荐