插槽(Slot)的使用方法

插槽是Vue.js中一个强大的功能,允许你在组件中预留位置,让父组件可以插入自定义内容。以下是插槽的主要使用方法:

基本插槽

<!-- 子组件 ChildComponent.vue -->
<template>
  <div>
    <h2>子组件标题</h2>
    <slot></slot>  <!-- 这里是插槽位置 -->
  </div>
</template>

<!-- 父组件使用 -->
<child-component>
  <p>这是插入到插槽中的内容</p>
</child-component>

具名插槽

<!-- 子组件 Layout.vue -->
<template>
  <div>
    <header>
      <slot name="header"></slot>
    </header>
    <main>
      <slot></slot>  <!-- 默认插槽 -->
    </main>
    <footer>
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

<!-- 父组件使用 -->
<layout>
  <template v-slot:header>
    <h1>这是页眉</h1>
  </template>
  
  <p>这是主内容</p>
  
  <template v-slot:footer>
    <p>这是页脚</p>
  </template>
</layout>

作用域插槽

允许子组件向插槽传递数据:

<!-- 子组件 TodoList.vue -->
<template>
  <ul>
    <li v-for="(item, index) in items" :key="index">
      <slot :item="item" :index="index"></slot>
    </li>
  </ul>
</template>

<!-- 父组件使用 -->
<todo-list :items="todos">
  <template v-slot:default="slotProps">
    <span>{{ slotProps.index + 1 }}. {{ slotProps.item.text }}</span>
  </template>
</todo-list>

缩写语法

  • v-slot:header 可以缩写为 #header
  • 默认插槽可以缩写为 #default
<layout>
  <template #header>
    <h1>缩写语法</h1>
  </template>
  
  <p>默认内容</p>
  
  <template #footer>
    <p>页脚内容</p>
  </template>
</layout>

动态插槽名

<base-layout>
  <template v-slot:[dynamicSlotName]>
    ...
  </template>
</base-layout>

插槽提供了灵活的组件内容分发机制,是构建可复用组件的重要工具。

### 插槽的基础概念 在 Vue.js 中,插槽Slot)提供了一种父组件向子组件传递 HTML 片段的方式。这不仅提高了组件的复用性、可读性和可扩展性,还让组件变得更加通用和灵活[^4]。 ### 默认插槽 最基础的形式是默认插槽,默认情况下任何放置于自定义标签内的内容都会被视作该组件内部预设 `<slot>` 的填充物: ```html <!-- 子组件 --> <template> <div class="container"> <!-- 此处会显示来自父级的内容 --> <slot></slot> </div> </template> <style scoped> .container { border: 1px solid black; } </style> ``` 当此组件被调用时,可以直接在其标签内写入想要展示的文字或HTML结构,它们会被自动插入到上述模板中的`<slot>`位置[^3]。 ### 具名插槽 为了更精确地控制多个不同区域的内容注入,Vue 支持具名插槽的概念。通过给 `<slot>` 添加 `name` 属性并指定名称,在父组件中就可以针对特定名字的空间填充值了: ```html <!-- 子组件 --> <template> <article> <header><slot name="header"></slot></header> <main><slot></slot></main> <!-- 默认插槽 --> <footer><slot name="footer"></slot></footer> </article> </template> ``` 而在使用这个多部分布局的组件时,则需利用带有特殊语法糖衣包裹的名字来指明具体要填充哪个部位的数据[^2]: ```html <MyComponent> <template v-slot:header> <h1>这里是头部标题</h1> </template> <p>这是主体文字。</p> <template #footer> <!-- 'v-slot:' 可省略为 '#' --> <small>© 2023</small> </template> </MyComponent> ``` ### 作用域插槽 除了静态内容外,有时还需要将某些数据从子组件暴露出来供外部操作,这时就轮到了作用域插槽出场。借助于此特性,可以在不破坏封装性的前提下共享状态信息[^1]。 #### 实现方式如下所示: - **子组件** ```html <ul> <li v-for="(item, index) in items" :key="index"> <slot :text="item.text"></slot> </li> </ul> <script> export default { data() { return { items: [ { text: "苹果" }, { text: "香蕉" } ] }; } }; </script> ``` - **父组件** ```html <ItemList> <template v-slot="{ text }"> <!-- 接收传来的参数 --> {{ text }} </template> </ItemList> ``` 这样做的好处在于保持了良好的解耦合度,同时也赋予了使用者更多的定制可能性[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值