【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

相关推荐
kyriewen112 分钟前
代码写成一锅粥?这5种设计模式让你的项目“起死回生”
前端·javascript·设计模式·typescript·ecmascript·html5
ywlovecjy4 分钟前
【Nginx 】Nginx 部署前端 vue 项目
前端·vue.js·nginx
skywalk81634 分钟前
g4f JavaScript调用报错问题解决
开发语言·javascript·ecmascript
Alice-YUE5 分钟前
AI对话为什么需要RAG
前端·语言模型·rag
C澒5 分钟前
IntelliPro 企业级产研协作平台:低代码实时预览与可视化编辑技术调研
前端·低代码·ai编程
霍理迪5 分钟前
TS类型断言和类型守卫
前端
木斯佳13 分钟前
前端八股文面经大全:京东前端实习一面(2026-04-16)·面经深度解析
前端
chenxu98b14 分钟前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
Bigger19 分钟前
第十章:我是如何剖析 CLI 里的终极 Agent 能力的(电脑控制与浏览器接管)
前端·claude·源码阅读
coder_liluyao21 分钟前
JS动画函数的封装(很实用)
javascript