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>
相关推荐
然我15 分钟前
不用 Redux 也能全局状态管理?看我用 useReducer+Context 搞个 Todo 应用
前端·javascript·react.js
前端小巷子20 分钟前
Web 实时通信:从短轮询到 WebSocket
前端·javascript·面试
神仙别闹23 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
web前端神器30 分钟前
指定阿里镜像原理
前端
枷锁—sha34 分钟前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
枷锁—sha36 分钟前
跨站请求伪造漏洞(CSRF)详解
运维·服务器·前端·web安全·网络安全·csrf
群联云防护小杜1 小时前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
DanB241 小时前
html复习
javascript·microsoft·html
汉得数字平台1 小时前
【鲲苍提效】全面洞察用户体验,助力打造高性能前端应用
前端·前端监控
花海如潮淹1 小时前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux