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

Vue组件的二次封装(案例)

一、不丢失原生组件的能力

不丢失原组件的任何功能,用户使用封装组件时依旧能用所有原 API

1.1 属性

import{ElInput,typeInputProps}from"element-plus"constprops=withDefaults(defineProps<InputProps>(),{inputStyle:()=>[],})

2.2 插槽

<el-input><templatev-for="(slotContent, name) in $slots"#[name]="slotData"><!-- 保留原有插槽内容 --><slot:name="name"v-bind="slotData"/></template></el-input>

2.3 事件

<!--父组件--><MyInputplaceholder="请输入"v-model="msg"type="text"ref="myInputRef"@input="handleChange"></MyInput>
<!--子组件--><el-inputv-bind="{ ...$attrs, ...props }"><templatev-for="(slotContent, name) in $slots"#[name]="slotData"><!-- 保留原有插槽内容 --><slot:name="name"v-bind="slotData"/></template></el-input>

2.4 返回方法

constvm=getCurrentInstance()functioncurrentRef(instance:any){if(vm){vm.exposed=instance||{}}}

二、封装

<!--父组件--> <template> <MyInput placeholder="请输入" v-model="msg" type="text" ref="myInputRef" @input="handleChange"> <template #append="slotData"> <div>{{ slotData }}</div> </template> <el-button @click="handleClear">点击我清除</el-button> </MyInput> </template> <script setup lang="ts"> import { ref } from "vue" import MyInput from "./components/index.vue" const myInputRef = ref() const msg = ref("1111111111") const handleClear = () => { if (!myInputRef.value) return myInputRef.value.clear() } const handleChange = () => { console.log(111) } </script>
<!---子组件--> <template> <div class="container"> <div>组件二次封装-{{ $attrs }}</div> <component :is="h(ElInput, { ...$attrs, ...props, ref: currentRef }, $slots)"></component> <slot></slot> </div> </template> <script setup lang="ts"> // inheritAttrs: false 是 Vue 组件中的一个配置项,用于控制是否自动将 $attrs 中的属性传递给组件的根元素。 defineOptions({ inheritAttrs: false, }) import { ElInput, type InputProps } from "element-plus" import { getCurrentInstance, h } from "vue" const props = withDefaults(defineProps<InputProps>(), { inputStyle: () => [], }) const vm = getCurrentInstance() function currentRef(instance: any) { if (vm) { vm.exposed = instance || {} } } const emit = defineEmits(["a"]) </script>
http://www.jsqmd.com/news/1154385/

相关文章:

  • 钢结构深化设计进度及质量管理办法
  • Windows Cleaner:彻底解决C盘爆红的免费开源优化工具
  • 【信息科学与工程学】【控制科学】第十四篇 网络控制01
  • STM32与PAM8904构建高效压电发声器驱动方案
  • 高精度ADC与STM32的数据采集系统设计与实现
  • 2026年深度横评:市面上那些“高情商聊天回复工具”,究竟是在帮你还是坑你?
  • 16位ADC与MCU的高精度信号采集方案解析
  • Hermes 对接飞书企业群完整配置指南:权限、群聊策略、@触发与常见故障排查
  • 钢结构深化设计要点
  • 钢结构深化设计重难点及合理化建议
  • BetterNCM安装器:让网易云音乐插件安装从未如此简单
  • AMD Ryzen硬件调试完全指南:免费开源工具SMUDebugTool让你的处理器性能飙升
  • 如何突破科学文库7天限制:CAJ文档解密完整指南
  • 创业者必须理解的技术指标:SLI、SLO与SLA的工程定义
  • WarcraftHelper终极指南:如何让经典魔兽争霸III在现代电脑上焕然新生
  • 2026收藏必备!小白也能看懂Agent的感知-规划-执行闭环,AI助理自主干活全解析
  • 【windows安装使用openclaw】
  • Winform DataGridView 5个高级封装方法实战:分页、样式、CRUD操作封装
  • 2026义乌最多跨境电商客户选择的独立站搭建公司排名:BBWEYY/比文云/Framer/Make/Brevo(2026年7月更新)含零代码SAAS、AI编程、源码定制交付
  • 基于STM32F745ZG与A3908的微米级运动控制方案
  • 腾讯游戏卡顿终结者:免费ACE-Guard限制器完整使用指南
  • WarcraftHelper:魔兽争霸3现代电脑完美运行完整指南
  • MySQL集群稳定运行的秘密武器:PilotGo-plugin-mysql安全检查与巡检实战
  • 原神成就一键导出终极指南:YaeAchievement让你轻松管理所有成就数据
  • 3步解锁Wand完整功能:免费获得专业版体验的终极指南
  • 高精度ADC信号链设计与STM32嵌入式系统优化
  • 基于PLC两部九层电梯设计(文档+讲解+电路图+仿真)
  • 让 AI 给你写个寓言
  • Sunshine游戏串流完整教程:5分钟搭建你的私人云游戏平台
  • TMC7300+PIC18F46K22有刷电机控制方案详解