slot插槽

本文详细介绍了Vue中插槽的概念,包括匿名插槽、具名插槽和作用域插槽的使用方法及原理。通过示例展示了如何在父组件中填充子组件的插槽内容,以及如何通过v-slot和slot-scope实现数据的传递。插槽机制使得组件间的内容定制和交互变得更加灵活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、什么是插槽?

插槽:子组件提供给父组件使用的一个占位符,用<slot></slot> 表示,
而父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签。


二、插槽的分类

一、匿名插槽
  1. 没有设置name属性的插槽,
  2. 一个组件中只能有一个匿名插槽,
  3. 匿名插槽只能作为没有slot属性的元素的插槽。
//child 子组件
<template>
    <div>
        <h5>我是子组件</h5>
        <slot>这是个匿名插槽(没有name属性)</slot>
    </div>
</template>
//parent 父组件
 <template>
    <div>
        <h3>我是一个父组件</h3>
       // <!--子组件内容 ↓ -->
        <child>
        //“这里是子组件内容!” 会将 “这是个匿名插槽(没有name属性)” 替换掉
           <div>
                这里是子组件内容!
            </div>
        </child>
    </div>
</template>
//展示页面
我是一个父组件
这里是子组件内容!
二、具名插槽

为插槽添加了name属性,可以添加多个聚名插槽

//child  子组件
<template>
  <div>
    <slot name="header"></slot>
    <h1>我是子组件</h1>
    <slot name="footer"></slot>
  </div>
</template>
//parent 父组件
<template>
	<Child>
		<span slot="header">我是header</span>
  		<span slot="footer">我是footer</span>
	</Child>
</template>
三、作用域插槽

父组件能接收来自子组件的slot传递过来的参数
作用域插槽必须放在template里面(父组件中)
template标签中的属性slot-scope="props"声明从子组件传递的数据都放一个自定义属性内。


插槽实现原理

当子组件vm实例化时,获取到父组件传入slot标签的内容,存放在 vm.$slot 中,默认插槽为vm.$slot.default, 具名插槽为vm.$slot.xxx,xxx 为插槽名,
当组件执行渲染函数时候,遇到slot标签,使用$slot(父组件传入slot标签的内容)中的内容进行替换,此时可以为插槽传递数据,若存在数据,则可称该插槽为作用域插槽。


三、v-slot、slot、slot-scope

一、 slot 的使用(匿名插槽&具名插槽)

查看上面的例子

二、 v-slot 的使用

  1. 在一个 <template>元素上使用 v-slot 指令,并以 v-slot 的参数形式提供其名称
  2. 与v-on 和 v-bind 一样,v-slot 也有缩写,即把 v-slot: 替换为字符 #。
<Child  #default="{ childData}">
  {{ childData.Name }}
</Child >

三、slot-scope的使用

//child 子组件 
<template class="child">
    <div>
       <slot name="default" :msg="msg"> </slot>
       <p>这里是child 组件</p>
    </div>
 </template>
//parent 
<template>
	<child>
		//作用域插槽的用法(slot-scope)
    	<div slot="default" slot-scope="childData">
          	{{ childData.msg }}
     	</div>
	</child >
<template>

### Vue.js Slot 插槽使用教程 #### 默认插槽 默认插槽是最简单的形式,它允许父组件向子组件传递任意内容。当子组件中只有一个未命的 `<slot>` 标签时,默认插槽会自动接收来自父组件的内容。 ```html <!-- 子组件 ChildComponent.vue --> <template> <div class="child"> <h2>这是子组件</h2> <slot></slot> </div> </template> <script> export default { name: 'ChildComponent' } </script> ``` ```html <!-- 父组件 ParentComponent.vue --> <template> <div class="parent"> <h1>这是父组件</h1> <ChildComponent> 这里是通过默认插槽传入的内容。 </ChildComponent> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent } } </script> ``` #### 具插槽插槽允许多个不同的位置插入内容。通过给 `<slot>` 添加 `name` 属性,在父组件中可以指定具体哪个具插槽应该被填充。 ```html <!-- 子组件 NamedSlotsExample.vue --> <template> <div class="named-slots-example"> <header> <slot name="header"></slot> </header> <main> <slot></slot> </main> <footer> <slot name="footer">这里是备用内容。</slot> </footer> </div> </template> ``` ```html <!-- 父组件 UsingNamedSlots.vue --> <template> <div class="using-named-slots"> <NamedSlotsExample> <!-- 向 header 字的 slot 填充内容 --> <template v-slot:header> <h1>这里是从父级传来的头部内容</h1> </template> <!-- 默认插槽内容 --> <p>这是一些普通的段落文本。</p> <!-- 向 footer 字的 slot 填充内容 --> <template #footer> <p>这里是从父级传来的底部内容</p> </template> </NamedSlotsExample> </div> </template> <script> import NamedSlotsExample from './NamedSlotsExample.vue'; export default { components: { NamedSlotsExample }, }; </script> ``` #### 作用域插槽 作用域插槽让子组件能够将数据暴露给父组件使用的模板片段内。这意味着可以在父组件中访问到子组件的数据并基于这些数据显示自定义内容。 ```html <!-- 子组件 ScopedSlotExample.vue --> <template> <ul> <li v-for="(item, index) in items" :key="index"> <slot :text="item.text">{{ item.text }}</slot> </li> </ul> </template> <script> export default { data() { return { items: [ { text: "苹果" }, { text: "香蕉" }, { text: "橙子" } ] }; } } </script> ``` ```html <!-- 父组件 UseScopedSlot.vue --> <template> <div class="use-scoped-slot"> <ScopedSlotExample v-slot:default="{ text }"> {{ text }} </ScopedSlotExample> </div> </template> <script> import ScopedSlotExample from "./ScopedSlotExample"; export default { components: { ScopedSlotExample } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值