Vue3 组件二次封装
2025-07-02 20:29:57
以 ElInput 为例
ElInputWrapper.vue
App.vue
<template>
<div class="my-el-input-wrapper">
<component :is="h(ElInput, { ...$attrs, ...$props, ref: changeRef }, $slots)"></component>
</div>
</template>
<script setup lang="ts">
import { h, getCurrentInstance } from 'vue';
import { ElInput, type InputProps, type InputEmits, type InputInstance } from 'element-plus';
defineProps<Partial<InputProps>>()
defineExpose<InputEmits>()
defineSlots<InputInstance['$slots']>()
const vm = getCurrentInstance()
function changeRef(inputInstance: InputInstance) {
vm.exposed = vm.exposeProxy = inputInstance || {}
}
</script>