2025-06-25vue学习

具名插槽

1、子组件

<div>
   <slot name="header"></slot>
   <slot></slot>
   <slot name="foot"></slot>
</div>

父组件

<ParentPage>
  <template #header><p>头部</p></template>
  <template #body><p>内容</p></template>
  <template #foot><p>结尾</p></template>
</ParentPage>

条件插槽

1、子组件

<div>
   <div v-if="$slots.header" class="header">
     <slot name="header"></slot>
  </div>
  <div v-if="$slots.default" class="body">
    <slot></slot>
   </div>
  <div v-if="$slots.foot" class="foot">
     <slot name="foot"></slot>
  </div>
</div>

父组件

<ParentPage>
  <template #header><p>头部</p></template>
  <template #body><p>内容</p></template>
  <template #foot><p>结尾</p></template>
</ParentPage>

向具名插槽中传入 props

子组件

<slot name="header" :title="msg"></slot>

<script setup>
const msg = ref('我是在子组件中定义的数据')
</script>

父组件

<ParentPage>
  <template #header="slotProps"><p>{{ slotProps.title }}</p></template>
</ParentPage>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Vue一文学会? Vue大家都知道就是一个国内非常流行的框架,最近因为过了许久没用Vue对于Vue的许多早已淡忘,...
    看物看雾阅读 630评论 0 3
  • 最近在学习Vue,下面是学习过程中总结了一些基本的知识点 Vue 一. 基本知识 1. 实例化 2. 过滤器 {{...
    冬天吃橘子_Autumn阅读 573评论 0 2
  • 构造器 每一个Vue.js的应用都是通过够构造函数Vue创建的一个Vue的根实例。例如: varvm =newVu...
    枫叶笛花阅读 450评论 0 0
  • 构造器 每一个Vue.js的应用都是通过够构造函数Vue创建的一个Vue的根实例。例如: 上述代码中在实例化vue...
    61ae56c48fc6阅读 5,454评论 2 4
  • highlight: a11y-dark 模板语法 1.属性绑定的值是 null或 undefined时,则该属性...
    Lafe_4b0a阅读 470评论 0 1