三种插槽的基本使用

本文介绍了Vue中插槽的使用,包括默认插槽、具名插槽和作用域插槽的区别,以及如何在父组件中动态分配内容。重点讲解了如何在子组件中定义插槽位置,以及如何通过slot-scope或v-slot来访问子组件的数据。

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

为什么要用插槽?

为了实现父组件每次使用组件时,有不一样的呈现

设置的时候比我们props传值更简单

子组件引用时可以写更加复杂的结构

简单地说就是你自定义的组件在被父组件引用时,本身是有一部分是给你开放的,里面是可以添加其他内容的,而不是完全由子组件内部做所有实现,如:

<child> 这里可以添加其他其它内容 </child>
例如:
<child> 🤩 </child>

 🤩在的位置就是插槽的位置,我们可以在子组件中去规定,这个🤩要在内部的那个地方呈现。

匿名插槽:

子组件:child.vue
<template>
  <div>
    <!--  默认插槽的位置在这 -->
    <slot></slot>
    <el-button type="success">相柳</el-button>
    <el-button type="info">檀健次</el-button>
  </div>
</template>
父组件:
<template>
  <div>
    <child>🤩</child>
    <upload-image></upload-image>
  </div>
</template>

solt其实并不是真的标签,它只是引用了父组件在使用该组件时,所设置的一个默认插槽内容,比如:

子组件:
<template>
  <div>
    <!--  默认插槽的位置在这 -->
    <slot>长相思</slot>
    <el-button type="success">相柳</el-button>
    <el-button type="info">檀健次</el-button>
  </div>
</template>
父组件:
<template>
  <div>
    <child>🤩</child>
    <!--  默认没有填写内容的子组件  -->
    <child></child>
    <upload-image></upload-image>
  </div>
</template>

当你在自定义子组件中多处位置放了插槽,那父组件中写入的内容要怎么区分是放在哪里的呢?

这时候就可以通过name属性给这些插槽做一个标记,这个插槽就叫具名插槽

具名插槽:

子组件:
<template>
  <div>
    <!--  默认插槽的位置在这 -->
    <slot>长相思</slot>
    <el-button type="success">相柳</el-button>
    <!--  具名插槽  -->
    <slot name="handsome">毒药</slot>
    <el-button type="info">檀健次</el-button>
  </div>
</template>
父组件:
<template>
  <div>
    <child></child>
    <upload-image></upload-image>
  </div>
</template>

 

slot写在每个指定插槽的外层时,选择template标签,因为 template 标签不会被解析,结构更清晰不多余

 使用: 父组件在要分发的标签里添加 

  • slot=”插槽名”取值用slot-scope,或者直接使用 scope,注意2.6.0.版本开始,已被废弃,我们可以使用v-slot)  
  • v-slot:插槽名(不写插槽名时,为默认插槽,相当于:v-slot:default),取值用  v-slot:插槽名=obj , 这里的obj就是对应插槽取到的数据
  •  #代替v-slot:   例如:v-slot:footer,可以写成#footer
父组件:
<template>
  <div>
    <child>🤩</child>
    <!--  默认没有填写内容的子组件  -->
    <child>
      <!--  <template slot="handsome">好好看!</template>  -->
      <!--  <template #handsome>好好看!</template>  -->
      <template v-slot:handsome>好好看!</template>
    </child>
    <upload-image></upload-image>
  </div>
</template>

 

除了上面两种插槽的使用,我们还可以在插槽里面使用子组件的一些数据,注意区分!!!子组件被引用时,双标签里面的内容仍然属于子组件的内容,不是父组件的内容。

如果子组件双标签里面的内容想要使用子组件的数据,就要用到作用域插槽啦

注意:默认的插槽和具名插槽、作用域插槽不能混合使用,使用的话,具名插槽、作用域插槽内容不会显示:

父组件:
<template>
  <div>
   <child>
        <!--v-slot相当于v-slot:default-->
        <template v-slot="obj">
            <h2>{{obj}}</h2>
            <!--下面的内容不会显示-->
            <template v-slot:handsome="obj2">
                <h2>{{obj2}}</h2>
            </template>
        </template>
    </child>
  </div>
</template>

作用域插槽(带有数据的插槽)

<template>
  <div>
    <!--  作用域插槽
    设置了一个属性address,绑定了子组件中的address值
    注意:这里只有在handsome插槽里面才可以使用这个数据-->
    <slot name="handsome" :address="address" >毒药</slot>
    <el-button type="info">檀健次</el-button>
  </div>
</template>
父组件:
<template>
  <div>
    <child>
      //也可用slot-scope属性获取子组件插槽handsome的数据 如slot-scope="obj"
      //下面三行代码意思一样
      <template slot="handsome" slot-scope="obj">{{obj}}</template>
      <template v-slot:handsome="obj">{{obj}}</template>
      <template #handsome="obj">{{obj}}</template>
    </child>
    <upload-image></upload-image>
  </div>
</template>

slot和v-solt的区别:

 v-slot 只能添加在 template 上 (或者独占默认插槽的缩写语法   v-slot:default="{ 插槽名 }")

接收到的是一个对象 

 可以利用对象.属性方式获取具体值,这里说一下解构赋值方式吧:

带不带引号都可以

<template #handsome="{address}">{{address}}</template>

 这篇博客也有详细使用说明

vue中slot插槽的使用_vue slot用法_苏苏哇哈哈的博客-优快云博客

Vuejs——(11)组件——slot内容分发_barnett_y的博客-优快云博客q

slot-scope简单了解

关于Vue中slot-scope插槽应用详细解读_kirinlau的博客-优快云博客

<think>好的,我需要帮助用户了解如何在Vue3中使用默认插槽。首先,我得回忆Vue3中关于插槽的知识点,特别是默认插槽使用方法。根据用户提供的引用内容,Vue3推荐使用v-slot语法糖,而默认插槽的作用域是父组件,内容由父组件定义。 首先,我应该明确默认插槽基本概念,即当子组件中有一个未命名的插槽时,父组件传入的任何内容都会填充到这个位置。然后需要给出具体的步骤,可能包括子组件如何定义插槽,父组件如何使用v-slot或者直接传递内容。 接下来,我需要考虑示例代码的结构。子组件部分可能需要使用<slot>标签来定义插槽的位置,而父组件则在调用子组件时,在标签内部放置内容。根据引用[2],默认插槽的内容作用域是父组件,所以示例中应该展示父组件的数据如何在插槽使用。 另外,用户可能需要知道多个默认插槽的情况,但根据引用[2],通常不建议这样做,而是使用具名插槽。所以需要提醒用户虽然技术上可以多个默认插槽,但最好使用具名插槽来区分。 还需要注意Vue2和Vue3的区别,用户提到Vue2使用slot和slot-scope,而Vue3使用v-slot。但默认插槽在Vue3中可以简写,比如使用#default或者直接放置内容而不需要包裹在template中,不过可能需要明确说明。 最后,确保示例代码正确,语法符合Vue3的标准,比如使用<script setup>语法,或者组合式API的写法。同时,要检查是否覆盖了用户可能遇到的常见问题,比如作用域、多个插槽的处理等。 总结步骤应该是: 1. 子组件定义默认插槽的位置。 2. 父组件传递内容到默认插槽。 3. 示例代码展示基本用法和作用域。 4. 注意事项,如作用域和多个插槽的情况。</think>### Vue3 默认插槽使用方法 #### 一、基本使用步骤 1. **子组件定义插槽** 在子组件模板中使用 `<slot>` 标签定义默认插槽位置: ```vue <!-- ChildComponent.vue --> <template> <div class="container"> <slot></slot> <!-- 默认插槽位置 --> </div> </template> ``` 2. **父组件传递内容** 父组件调用子组件时,直接在组件标签内部写入内容,或使用 `<template #default>` 包裹: ```vue <!-- ParentComponent.vue --> <template> <ChildComponent> <p>这是插入默认插槽的内容</p> </ChildComponent> </template> ``` #### 二、完整示例 ```vue <!-- 子组件 ChildComponent.vue --> <template> <div class="card"> <slot>默认备用内容(当父组件未传内容时显示)</slot> </div> </template> ``` ```vue <!-- 父组件 ParentComponent.vue --> <template> <ChildComponent> <!-- 覆盖默认插槽内容 --> <div class="custom-content"> <h3>标题(父组件定义)</h3> <p>{{ parentMessage }}</p> <!-- 访问父组件数据 --> </div> </ChildComponent> </template> <script setup> import ChildComponent from './ChildComponent.vue'; const parentMessage = '来自父组件的信息'; </script> ``` #### 三、核心特性说明 1. **作用域规则** 默认插槽内容直接使用父组件的数据和方法[^2]。 2. **备用内容机制** 若父组件未提供内容,则显示 `<slot>` 标签内的默认内容。 3. **动态内容支持** 可通过响应式数据或条件渲染控制插槽内容: ```vue <ChildComponent> <template v-if="showSlot"> <span>动态显示的内容</span> </template> </ChildComponent> ``` #### 四、注意事项 1. **避免多个默认插槽** Vue3 会合并多个 `<slot>`,但推荐使用具名插槽区分不同区域[^2]。 2. **简洁语法** 默认插槽无需显式使用 `<template #default>`,直接传递内容更简洁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值