Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue 具名插槽

作者:迷失未来   发布日期:2025-11-27   浏览:92

<template>
  <div>
    <!-- 定义一个包含具名插槽的组件 -->
    <slot-component>
      <!-- 默认插槽内容 -->
      <template v-slot:default>
        <p>这是默认插槽的内容。</p>
      </template>

      <!-- 具名插槽 content 插槽 -->
      <template v-slot:content>
        <p>这是具名插槽 "content" 的内容。</p>
      </template>

      <!-- 具名插槽 footer 插槽 -->
      <template v-slot:footer>
        <p>这是具名插槽 "footer" 的内容。</p>
      </template>
    </slot-component>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<!-- slot-component.vue -->
<template>
  <div class="slot-component">
    <!-- 默认插槽 -->
    <slot></slot>

    <!-- 具名插槽 content -->
    <div class="content-slot">
      <slot name="content"></slot>
    </div>

    <!-- 具名插槽 footer -->
    <footer class="footer-slot">
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

<script>
export default {
  name: 'SlotComponent'
}
</script>

解释说明:

  1. slot-component.vue:定义了一个包含具名插槽的组件。

    • 使用 <slot> 标签定义默认插槽。
    • 使用 <slot name="content"><slot name="footer"> 分别定义了名为 contentfooter 的具名插槽。
  2. App.vue:使用 slot-component 组件,并通过 v-slot 指令为不同的插槽提供内容。

    • v-slot:default 用于提供默认插槽的内容。
    • v-slot:contentv-slot:footer 分别用于提供具名插槽 contentfooter 的内容。

上一篇:vue3 script setup语法糖

下一篇:vue换行符

大家都在看

vue.js devtools用法

vue js for循环

highlight.js vue

vue.config.js 配置

vue.config.js 配置代理

vue.config.js configu

node.js vue

vue3组件传值的方式

vue3 子路由

vue3 router传参

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站