【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

相关推荐
雨雨雨雨雨别下啦几秒前
Vue案例——面经
前端·javascript·vue.js
oo1213824 分钟前
里程碑5 - 完成框架 npm 包抽象封装并发布
前端·npm
达拉24 分钟前
我花了三天用AI写了个上一代前端构建工具
前端·前端工程化
bysking28 分钟前
【31-Ai-Agent】ai-agent的核心实现细节-bysking
前端
从文处安32 分钟前
「前端何去何从」(React教程)React 状态管理:从局部 State 到可扩展架构
前端·react.js
一拳不是超人35 分钟前
Three.js一起学-如何通过官方例子高效学习 Three.js?手把手带你“抄”出一个3D动画
前端·webgl·three.js
椰子皮啊39 分钟前
400行Node.js搞定mediasoup信令转换:一次跨语言"表白"实录
前端·架构
果然_1 小时前
告别混淆!Git 多账号按域名/目录自动切换身份的终极指南
前端
Wect1 小时前
React Scheduler & Lane 详解
前端·react.js·面试
myNameGL1 小时前
ArkTs核心语法
前端·javascript·vue.js