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>
相关推荐
顺丰同城前端技术团队7 分钟前
DeepSeek 国产大模型新标杆
前端·后端·程序员
Java水解8 分钟前
微前端架构:从单体到模块化的前端新革命
前端
Mr_汪11 分钟前
uniapp使用h5的map(已弃用)
前端
前端进阶者14 分钟前
vite调试node_modules下面插件
前端·vite
YaHuiLiang21 分钟前
小微互联网公司与互联网创业公司 -- 学历之殇
前端·后端·面试
用户261245834016124 分钟前
vue学习路线(11.watch对比computed)
前端·vue.js
CAD老兵30 分钟前
前端 Source Map 原理与结构详解
前端
gnip34 分钟前
markdown预览自定义扩展实现
前端·javascript
大猫会长1 小时前
mac中创建 .command 文件,执行node服务
前端·chrome
旧时光_1 小时前
Zustand 状态管理库完全指南 - 进阶篇
前端·react.js