vue2版本中slot的基本使用详解

前言

在vue的开发过程中,我们会经常使用到vue的slot插槽组件,vue官方文档的描述:

Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将 元素作为承载分发内容的出口

slot大概分为以下几种:

基础slot组件(匿名插槽)

匿名插槽主要使用场景并不涉及特别复杂的业务,更像是纯展示组件内容

<!--子组件-->

<template>
	<span>
		我是基础slot子组件, 父组件传过来的值:
		<span style="color: red"><slot></slot></span>
	</span>
</template>

<!--父组件-->

<li>
    基础slot组件(匿名插槽):<Base>这是一段父组件传过来的文字</Base>
</li>

import Base from "./Base.vue";

具名插槽

具名插槽,需要在父组件和子组件约定插槽名称

<!--子组件-->

<template>
	<span>
		<span style="color: red">
			<slot name="name1"></slot>
			<slot name="name2"></slot>
		</span>
	</span>
</template>

<!--父组件-->

<li>
    <p>具名插槽:</p>
    <Specific>
    	<template v-slot:name1>
    		<p>name1传过来的内容</p>
    	</template>
    	<template v-slot:name2>
    		<p>name2传过来的内容</p>
    	</template>
    </Specific>
</li>

import Specific from "./Specific.vue";

作用域插槽

作用域插槽,子组件提供数据,父组件接收子组件的值并展示和处理逻辑

<!--子组件-->

<template>
	<span>
		<span>
			<slot name="scopeName" v-bind:scopeData="age"></slot>
		</span>
	</span>
</template>

<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";

@Component
export default class Scope extends Vue {
	private age: Number = 23;
}
</script>

<!--父组件-->

<li>
	<p>作用域插槽</p>
	<Scope>
		<template v-slot:scopeName="childData">
			作用域子组件slot返回的数据:
			<span style="color: red">
				{{ childData.scopeData }}
			</span>
		</template>
	</Scope>
</li>

import Specific from "./Specific.vue";

解构插槽

解构插槽,类似在js书写对象过程中的对象解构

{ data:{ username:1 } }
<!--子组件-->

<template>
	<span>
		<p>
			<slot v-bind:user="user"></slot>
		</p>
	</span>
</template>

<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";

@Component
export default class Deconstru extends Vue {
	private user: Object = {
		name: "zhangsan",
		age: 23,
	};
}
</script>

<!--父组件-->

<li>
	<p>解构插槽</p>
	<Deconstru>
		<template v-slot="{ user: person }">
			父组件模板:{{ person.name }},{{ person.age }}
		</template>
	</Deconstru>
</li>

import Specific from "./Deconstru.vue";

以上例子均已上传至开源仓库,后续关于vue的学习笔记均会更在在该项目上,欢迎star


总结

在vue的插槽文档中,还有包含

<template #footer>
    <p>Here's some contact info</p>
  </template>

详细关于插槽的官方文档传送门

https://cn.vuejs.org/v2/guide/components-slots.html

文章个人博客地址:vue2版本中slot的基本使用详解

欢迎关注公众号:程序猿布欧,不定期更新一些前端入门文章

创作不易,转载请注明出处和作者。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue,插槽(slot)是一种用于组件之间内容分发的机制。它允许父组件向子组件传递内容,使得子组件可以根据需要展示不同的内容。插槽可以用于传递任意类型的内容,包括HTML、文本和其他组件。 Vue的插槽有三种使用方式:slotslot-scope和v-slot。 1. slotslot标签是最基本的插槽使用方式。父组件可以在子组件的slot标签放置内容,这些内容将会替换子组件具名插槽的位置。例如: ```html <!-- 父组件 --> <template> <child-component> <template v-slot:header> <h1>这是标题</h1> </template> </child-component> </template> <!-- 子组件 --> <template> <div> <slot name="header"></slot> <!-- 其他子组件内容 --> </div> </template> ``` 上述代码,父组件在子组件的插槽放置了一个标题,子组件通过`<slot name="header"></slot>`来展示这个插槽的内容。 2. slot-scope:slot-scope指令用于在插槽接收父组件传递的数据。通过这种方式,子组件可以访问父组件的数据,并在插槽进行处理。例如: ```html <!-- 父组件 --> <template> <child-component> <template v-slot:default="slotProps"> <h1>{{ slotProps.title }}</h1> <p>{{ slotProps.content }}</p> </template> </child-component> </template> <!-- 子组件 --> <template> <div> <slot :title="title" :content="content"></slot> <!-- 其他子组件内容 --> </div> </template> <script> export default { data() { return { title: '这是标题', content: '这是内容' }; } }; </script> ``` 在上述代码,父组件通过`v-slot:default="slotProps"`将数据传递给子组件的插槽,并在插槽使用`slotProps`来访问这些数据。 3. v-slot:v-slot指令是Vue2.6版本及以上引入的新特性,用于简化插槽的语法。它可以直接在子组件上使用,而不需要使用template标签。例如: ```html <!-- 父组件 --> <template> <child-component> <template #header> <h1>这是标题</h1> </template> </child-component> </template> <!-- 子组件 --> <template> <div> <slot name="header"></slot> <!-- 其他子组件内容 --> </div> </template> ``` 在上述代码,父组件使用`#header`来定义插槽,并在子组件使用`<slot name="header"></slot>`来展示插槽的内容。 总结一下,slot用于定义插槽的位置,slot-scope用于接收父组件传递的数据,并在插槽进行处理,v-slot是对插槽语法的简化。这些插槽的使用方式可以根据具体需求选择适合的方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值