【vue】slot 匿名插槽 / 具名插槽

  • slot
  • 父组件向子组件传递数据

匿名插槽--直接写


具名插槽--指定名称

父组件中

子组件中:

代码

App.vue

html 复制代码
<template>
  <h2>App.vue</h2>
  <!-- 匿名插槽 -->
  <Header>
    <a href="1234567890.com">1234567890</a>
  </Header>
  <!-- 具名插槽 -->
  <Footer>
    <template v-slot:url>
      <a href="abcdefg.com">abcdefg</a>
    </template>
  </Footer>

</template>

<script setup>
import { ref, reactive, provide } from "vue";

import Header from "./components/Header.vue";
import Footer from "./components/Footer.vue";

</script>

<style lang="scss" scoped></style>

Header.vue

html 复制代码
<template>
    <h2>Header.vue</h2>
    <slot/>
</template>

<script setup>
import { inject } from 'vue'

</script>

<style lang="scss" scoped></style>

Foot.vue

html 复制代码
<template>
    <h2>Footer.vue</h2>
    <slot name="url"/>
</template>

<script setup>

</script>

<style lang="scss" scoped>

</style>

参考

https://www.bilibili.com/video/BV1nV411Q7RX

相关推荐
程序员大阳1 小时前
副队长CSS教程(30)–CSS Grid 实战:实现复杂自适应表单布局
前端·css·自适应·grid·表单
Csvn1 小时前
微前端实现
前端
Chrischaan2 小时前
做 AI 塔罗小程序时,我为什么不让大模型自己抽牌:一次上线复盘
前端
骑着蜗牛撵大象3272 小时前
LangChain 工具调用从声明到闭环:给智能体接上真实的手脚
前端
数据掘金2 小时前
百度统计后台页面数据异常?从手动调用时机开始排查
前端
hunteritself2 小时前
10 层 4K 实时剪辑,国产工作站进入广电制作核心
大数据·前端·人工智能·深度学习·transformer
踩着两条虫2 小时前
我在浏览器里“盖”了一个村:Vue + Three.js 大屏开发踩坑记
前端·vue.js·three.js
默_笙2 小时前
🧀 让大模型的答案"流"出来:NestJS AI 模块 + SSE + RxJS 实战
javascript·人工智能
mbshqqb2 小时前
TypeScript基础教程之同步生成器
前端·javascript·typescript
嘟嘟同学和妮妮同学3 小时前
【升级】一个中国历史可视化站:皇帝时间线梳理、古代思想流派、诗词鉴赏 (2)
前端·html