Vue中的Slot

本文介绍了Vue中Slot的概念及其用途,重点讲解了如何通过Slot实现父组件向子组件传递动态内容。通过示例展示了默认Slot和命名Slot的用法,以及在封装组件时如何利用Slot实现不同模块的灵活配置。示例中展示了如何在BaseLayout组件中定义多个Slot,并在实际使用时插入自定义内容。
摘要由CSDN通过智能技术生成

作用:父组件向子组件传递模板(原理似props)

何时用:封装组件,组件中某一模块不固定时

slot中有内容时,父组件不传值,用默认内容

                               负组件传值,用负组件中的

<button type="submit">
  <slot>
    Submit <!-- fallback content -->
  </slot>
</button>

可添加命名槽,父组件调用时,传入name

<div class="container">
  <header>
    <slot name="header"></slot>
  </header>
  <main>
    <slot></slot>
  </main>
  <footer>
    <slot name="footer"></slot>
  </footer>
</div>

使用

<BaseLayout>
  <template #header>
    <h1>Here might be a page title</h1>
  </template>

  <template #default>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </template>

  <template #footer>
    <p>Here's some contact info</p>
  </template>
</BaseLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值