vue组件二次封装后暴露子组件方法

demo.vue

javascript 复制代码
<template>
    <el-button type="primary" @click="focus">聚焦{{ msg }}</el-button>
    <MyInput v-model="msg" ref="myInputRef">
        <template #prepend>
            <el-button>prefix</el-button>
        </template>
        <template #append>
            <el-button>append</el-button>
        </template>
    </MyInput>
</template>
<script setup lang="ts">
import MyInput from "./MyInput.vue"
import { ref } from "vue"
const msg = ref('111')
const myInputRef = ref()
const focus = () => {
    myInputRef.value.focus()
}
</script>

MyInput.vue

javascript 复制代码
<template>
    <div>nihao</div>
    <el-input ref="inputRef" v-bind="$attrs">
        <template v-for="(, slot) in $slots" :key="slot" #[slot]="slotProps">
            <slot :name="slot" v-bind="slotProps" />
        </template>
        <!-- 或者 -->
        <!-- <template v-for="(slotContent, slotName) in $slots" :key="slotName" #[slotName]="slotProps">
            <slot :name="slotName" v-bind="slotProps">{{ slotContent }}</slot>
        </template> -->
    </el-input>
</template>
<script setup lang="ts">
import { ref } from "vue"
const inputRef = ref()
defineExpose(
    new Proxy(
        {},
        {
            get(target, key) {
                return inputRef.value?.[key]
            },
            has(target, key) {
                return key in inputRef.value
            }
        })
)

</script>
相关推荐
java1234_小锋1 小时前
Vue3专题 - 组件基础
vue.js·vite
圣殿骑士-Khtangc1 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
BigTopOne2 小时前
【ijkplayer】 硬解码流程
前端
kyriewen2 小时前
DeepSeek Harness开源第一天我就上手了——和Claude Code的差距比想象中大
前端·ai编程·deepseek
捡田螺的小男孩3 小时前
什么是 Skill?手把手带你写一个简单有用的 Skill!
前端·后端·程序员
IT_陈寒3 小时前
Redis集群这个坑,差点让我通宵
前端·人工智能·后端
90后的晨仔3 小时前
uni-app 路由跳转与页面导航 —— 终极详解(对标 iOS / Android / 鸿蒙三端原生)
前端
Heo3 小时前
大厂前端调试不能只会debugger
前端·javascript·面试
用户2181697049303 小时前
Flutter (十七) 网络请求
前端
cidy_983 小时前
React 19 + Vite 企业级前端项目:从零搭建到规范交付
前端