【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

相关推荐
共享家95271 天前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法
摘星编程1 天前
OpenHarmony环境下React Native:自定义useTruncate文本截断
javascript·react native·react.js
Duang007_1 天前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
有来技术1 天前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5161 天前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
2601_949868361 天前
Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
开发语言·javascript·flutter
m0_748229991 天前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
C澒1 天前
前端监控系统的最佳实践
前端·安全·运维开发
xiaoxue..1 天前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试