【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

相关推荐
O***p6041 分钟前
当“前端虚拟化”成为可能:构建下一代 Web 应用的新范式
前端
孤酒独酌17 分钟前
一次断网重连引发的「模块加载缓存」攻坚战
前端
jinzeming99921 分钟前
Vue3 PDF 预览组件设计与实现分析
前端
NuLL21 分钟前
全场景智能克隆工具:超越 JSON.parse(JSON.stringify())
javascript
编程小Y22 分钟前
Vue 3 + Vite
前端·javascript·vue.js
GDAL31 分钟前
前端保存用户登录信息 深入全面讲解
前端·状态模式
大菜菜37 分钟前
Molecule Framework -EditorService API 详细文档
前端
Anita_Sun39 分钟前
😋 核心原理篇:线程池的 5 大核心组件
前端·node.js
灼华_43 分钟前
Web前端移动端开发常见问题及解决方案(完整版)
前端
_请输入用户名44 分钟前
Vue3 Patch 全过程
前端·vue.js