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

组件库二次封装——透传问题

属性和事件透传

属性和事件的透传需要借助一个内置 api $attrs

<my-input a="1" b="2" @change="() => {}" />
<template><div class="input-wrapper">输入框:<el-input v-bind="$attrs"></el-input></div>
</template><script lang="ts">
export default {mounted() {console.log("attrs", this.$attrs)}
}
</script>

在这里插入图片描述

但是这样直接使用 $attrs 透传的优先级会比组件自身的属性和事件优先级低。比如:

<template><div class="input-wrapper">输入框:<el-input v-bind="$attrs"></el-input></div>
</template><script lang="ts">
export default {props: ['a'],emits: ['change'],mounted() {console.log("attrs", this.$attrs)}
}
</script>

在这里插入图片描述

插槽透传

一般我们需要透传插槽的话,需要:

  <my-input v-model="text" ><template #prepend>prepend</template><template #append>append</template><template #suffix>suffix</template><template #prefix>prefix</template></my-input>
<template><div class="input-wrapper">输入框:<el-input v-bind="$attrs"><template #prepend><slot name="prepend"></slot></template><template #append><slot name="append"></slot></template><template #suffix><slot name="suffix"></slot></template><template #prefix><slot name="prefix"></slot></template></el-input></div>
</template>

在这里插入图片描述

这样写有一些问题,一是繁琐,二是可能传递的插槽个数不同,导致不同的渲染逻辑(业务要求)。所以我们需要使用 $slots 这个 api。

插槽的本质是一个函数。

  <my-input v-model="text" ><template #prepend>prepend</template><template #append>append</template></my-input>
<template><div class="input-wrapper">输入框:<el-input v-bind="$attrs"><template v-for="(_, name) in $slots" #[name]="scopedData"><slot :name="name" v-bind="scopedData"/></template></el-input></div>
</template><script lang="ts">
export default {mounted() {console.log("slots", this.$slots)}
}
</script>

在这里插入图片描述

ref 透传

在 vue 中 ref 是无法透传的,在 react 中可以通过 forwardRef 获取子组件。

所以我们只能将子组件内部的原组件库 ref 挂载到外部 二次封装的组件 ref 上。

<template><div class="input-wrapper">输入框:<el-input ref="elInput" v-bind="$attrs"><template v-for="(_, name) in $slots" #[name]="scopedData"><slot :name="name" v-bind="scopedData"/></template></el-input></div>
</template><script>
export default {mounted() {console.log("elInput", this.$refs.elInput)for (const elInputKey in this.$refs.elInput) {this[elInputKey] = this.$refs.elInput[elInputKey];}}
}
</script>
http://www.xdnf.cn/news/866449.html

相关文章:

  • UniApp 全生命周期钩子详解
  • 数据标注与大模型的双向赋能:效率与性能的跃升
  • CMake + Ninja 构建程序示例
  • CortexON:开源的多代理AI系统无缝自动化和简化日常任务
  • 【推荐算法】推荐系统核心算法深度解析:协同过滤 Collaborative Filtering
  • 07 APP 自动化- appium+pytest+allure框架封装
  • RabbitMQ 的异步化、解耦和流量削峰三大核心机制
  • ④Pybullet之Informed RRT*算法介绍及示例
  • 在本地查看服务器上的TensorBoard
  • Git安装与常用命令全攻略
  • Elasticsearch的搜索流程描述
  • 分类与逻辑回归 - 一个完整的guide
  • Git常用命令完全指南:从入门到精通
  • 正则表达式检测文件类型是否为视频或图片
  • 海信IP810N-海思MV320芯片-安卓9-2+16G-免拆优盘卡刷固件包
  • 【Android】RV折叠适配器
  • 基于大模型的结节性甲状腺肿智能诊疗系统技术方案
  • NLP学习路线图(二十三):长短期记忆网络(LSTM)
  • constexpr 是 C++11 引入的关键字
  • PocketFlow 快速入门指南
  • 阿里内推-6月新出HC
  • App 上线后还能加固吗?iOS 应用的动态安全补强方案实战分享(含 Ipa Guard 等工具组合)
  • 数学复习笔记 26
  • 雷卯针对易百纳G610Q-IPC-38E 模组防雷防静电方案
  • 2025年大模型平台落地实践研究报告|附75页PDF文件下载
  • MySQL 索引底层原理剖析:B+ 树结构、索引创建维护与性能优化策略全解读
  • x86 汇编逻辑运算全解析:从【位操作】到实际应用(AND,OR,NOT,XOR,TEST)
  • 缓存控制HTTP标头设置为“无缓存、无存储、必须重新验证”
  • Cursor 工具项目构建指南: Web Vue-Element UI 环境下的 Prompt Rules 约束(new Vue 方式)
  • 杰发科技AC7801——使用内部晶振