当前位置: 首页 > news >正文

vue 插槽Slots

vue插槽官网

<button class="fancy-btn"><slot></slot> <!-- 插槽出口 -->
</button>

<slot> 元素是一个插槽出口 (slot outlet),标示了父元素提供的插槽内容 (slot content) 将在哪里被渲染。

// 定义一个Child.vue 子组件
<template><section class="child"><h2>我是Child组件</h2>// 匿名插槽(默认插槽)<slot></slot>// 如果子组件中没有使用插槽,父组件如果需要往子组件中填充模板内容或者html片段, 是没无法实现的// 具名插槽(有名字的 named slots)<h2>header插槽</h2><header><slot name="header"></slot></header>// 传递参数给插槽<main><slot name="main" :dataSlot="data"></slot></main><h2>footer插槽</h2><footer><slot name="footer"></slot></footer><!-- 传递props的插槽 --><div><slot name="propSlot" :text="greetingText" :count="1"></slot></div>// 动态插槽<template v-slot:[dynamicSlotName]>...</template><!-- 缩写为 --><template #[dynamicSlotName]>...</template></section>
</template><script setup>import { ref } from 'vue'
const greetingText = ref('hello')
const data = ref(['Apple', 'Peach', 'Mango', 'Banana', 'Cherry'])</script>
// 定义一个Parent.vue
<template><section class="parent"><h2>我是Parent组件</h2><Child>// 默认插槽<section>{{ msg }}</section>// 父组件填充内容时通过 v-slot:[name] 的方式指定到对应的插槽中// slot 存在于子组件,它有个name属性,用于指定插槽的名字// v-slot 指令用在父组件(或者使用参数形式 v-slot:footer, 也可以简写为 #footer),最终页面展示结果是父组件// v-slot 只能用在组件或者 template 标签上// 具名插槽 <slot name="name"></slot><template v-slot:header><h3>{{ headerText }}</h3></template>// 作用域插槽<template #main="{ slotData }"><ul v-for="item in slotData" :key="item"><li>{{ item }}</li></ul></template><template #footer><h3>{{ footerText }}</h3></template>// 接收slot传递的props<template #propSlot="{ text, count }"><div>{{ text }} {{ count }}</div></template></Child></section>
</template><script setup>
import { ref } from 'vue'import Child from './Child.vue';const msg = ref("我是子组件插槽的一个 默认 内容")const headerText = ref('我是子组件的 header 内容')const footerText = ref('我是子组件的 footer 内容')</script>
http://www.lryc.cn/news/155427.html

相关文章:

  • 论文阅读《Nougat:Neural Optical Understanding for Academic Documents》
  • 较难的换根dp:P6213 「SWTR-04」Collecting Coins
  • Springboot - 15.二级分布式缓存集成-Caffeine
  • 二叉树的介绍及二叉树的链式结构的实现(C语言版)
  • 不同写法的性能差异
  • Bytebase 2.7.0 - ​新增分支(Branching)功能
  • day55 动规.p15 子序列
  • TypeScript DOM类型的声明
  • springboot找不到注册的bean
  • MEMS传感器的原理与构造——单片式硅陀螺仪
  • Redis集群服务器
  • 动态维护直径 || 动态维护树上路径 || 涉及LCA点转序列 || 对欧拉环游序用数据结构维护:1192B
  • MySQL 存储引擎,你了解几个?
  • Java 动态规划 Leetcode 740. 删除并获得点数
  • 算法通关村十三关-青铜:数字与数学基础问题
  • 猜拳游戏小程序源码 大转盘积分游戏小程序源码 积分游戏小程序源码
  • 【Python】爬虫练习-爬取豆瓣网电影评论用户的观影习惯数据
  • webpack基础配置【总结】
  • typescript 支持与本地调试
  • 后端面试话术集锦第 十八 篇:JVM面试话术
  • “历久弥新 | 用AI修复亚运珍贵史料”活动震撼来袭!
  • uni-app 之 scroll-view和swiper
  • Harmony网络请求工具类
  • 【Python 自动化】自媒体剪辑第一版·思路简述与技术方案
  • 【前端】webpack打包去除console.log
  • docker使用(二)提交到dockerhub springboot制作镜像
  • antd中Popover 气泡卡片样式修改
  • 3月面试华为被刷,准备半年,9月二战华为终于上岸,要个27K不过分吧?
  • Kali之BurpSuite_pro安装配置
  • 双指针算法总结