当前位置: 首页 > article >正文

11 vue3之插槽全家桶

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签。

匿名插槽

1.在子组件放置一个插槽

<template>
    <div>
       <slot></slot>
    </div>
</template>

 父组件使用插槽

在父组件给这个插槽填充内容

        <Dialog>
           <template v-slot>
               <div>2132</div>
           </template>
        </Dialog>

具名插槽

具名插槽其实就是给插槽取个名字。一个子组件可以放多个插槽,而且可以放在不同的地方,而父组件填充内容时,可以根据这个名字把内容填充到对应插槽中

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

父组件使用需对应名称

        <Dialog>
            <template v-slot:header>
               <div>1</div>
           </template>
           <template v-slot>
               <div>2</div>
           </template>
           <template v-slot:footer>
               <div>3</div>
           </template>
        </Dialog>

插槽简写

        <Dialog>
            <template #header>
               <div>1</div>
           </template>
           <template #default>
               <div>2</div>
           </template>
           <template #footer>
               <div>3</div>
           </template>
        </Dialog>

作用域插槽

作用域插槽由子组件暴露值出来

在子组件动态绑定参数 派发给父组件的slot去使用

    <div>
        <slot name="header"></slot>
        <div>
            <div v-for="item in 100">
                <slot :data="item"></slot>
            </div>
        </div>
 
        <slot name="footer"></slot>
    </div>

 通过结构方式取值

         <Dialog>
            <template #header>
                <div>1</div>
            </template>
            <template #default="{ data }">
                <div>{{ data }}</div>
            </template>
            <template #footer>
                <div>3</div>
            </template>
        </Dialog>

动态插槽

插槽可以是一个变量名

<template>
  <h2>动态插槽动态更改插槽名</h2>
  <Ten>
    <template #[name]>
      <div>我在哪儿</div>
    </template>
  </Ten>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import Ten from "./components/ten.vue";
// let name = ref("header");
let name = ref("footer");
// let name = ref("default");
</script>

 子组件

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

<style lang="scss" scoped>
div {
  width: 100%;
  height: 100px;
}
.header {
  background: antiquewhite;
}
.main {
  background: aqua;
}
.footer {
  background: pink;
}
</style>

效果图:

12 vue3之异步组件&代码分包&内置组件suspense和teleport-CSDN博客文章浏览阅读23次。12 vue3之异步组件&代码分包&内置组件suspense和teleporthttps://blog.csdn.net/qq_37550440/article/details/142329387?sharetype=blogdetail&sharerId=142329387&sharerefer=PC&sharesource=qq_37550440&spm=1011.2480.3001.8118


http://www.kler.cn/news/310109.html

相关文章:

  • 【JavaEE】多线程编程引入——认识Thread类
  • mysql怎样优化count(*) from 表名 where …… or ……这种慢sql
  • c++基础入门三
  • Java Web开发中处理Response返回值的技巧与实践
  • 1×1卷积核【super star 卷积核】
  • 【数据结构-线段树】【差分】力扣732. 我的日程安排表 III
  • 基于PHP的丽江旅游管理系统
  • VMware ESXi 7.0U3q macOS Unlocker 集成驱动版更新 OEM BIOS 2.7 支持 Windows Server 2025
  • STM32快速复习(十二)FLASH闪存的读写
  • [Meachines] [Medium] Bart Server Monitor+Internal Chat+UA投毒+Winlogon用户密码泄露权限提升
  • MySQL高阶1777-每家商店的产品价格
  • 文心智能体 恐怖类游戏
  • 一.Oracle每日运维操作
  • bug | pycharm社区版无sciview解决办法
  • JVM 调优篇7 调优案例1-堆空间的优化解决
  • Holynix: v1
  • 基于SSM的在线家用电器销售系统
  • 【ARM】Trustzone和安全架构
  • [SDX35+WCN6856]SDX35 + WCN6856 WiFi导致系统crash问题分析及解决方案
  • (娱乐)魔改浏览器-任务栏图标右上角加提示徽章
  • 线性链条件随机场(Linear Chain Conditional Random Field)-有监督学习方法、概率模型、生成模型、对数线性模型、参数化模型
  • 力扣(LeetCode)每日一题 1184. 公交站间的距离
  • 前后端分离Vue美容店会员信息管理系统o7grs
  • Java-使用反射来处理对象,并构建新的JSON数据结构
  • 换个手机IP地址是不是不一样?
  • spring boot admin集成,springboot2.x集成监控
  • .net core8 使用JWT鉴权(附当前源码)
  • Python 之数据库操作(Python Database Operations)
  • Linux(ubuntu)(c语言程序)
  • C++(C++的文件I/O)