Vue3 组件二次封装

2025-07-02 20:29:57

以 ElInput 为例

ElInputWrapper.vue
App.vue
Playground
          <template>
  <div class="my-el-input-wrapper">
    <component :is="h(ElInput, { ...$attrs, ...$props, ref: changeRef }, $slots)"></component>
  </div>
</template>

<script setup lang="ts">
import { ElInput, type InputEmits, type InputInstance, type InputProps } from 'element-plus';
import { getCurrentInstance, h } from 'vue';

defineProps<Partial<InputProps>>()
defineExpose<InputEmits>()
defineSlots<InputInstance['$slots']>()

const vm = getCurrentInstance()

function changeRef(inputInstance: InputInstance) {
  vm.exposed = vm.exposeProxy = inputInstance || {}
}
</script>

        
© 2021-2026 sunshj's Blog