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>
相关推荐
万少3 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站5 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名8 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫8 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊8 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter8 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折8 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_8 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
不会敲代码19 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Angelial9 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js