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>
相关推荐
深蓝AI3 小时前
Meta 开源用了八年的设计系统 astryx:连坐两天 GitHub 总榜第一,组件库开始写给 agent 的说明书
前端·人工智能
ruanno_top3 小时前
轮播每 3 秒自己翻页:键盘停不下来,读屏也不知道换了页
vue.js
IT_陈寒4 小时前
Vue的watch把我坑惨了,原来要这样用才靠谱
前端·人工智能·后端
1-1=04 小时前
Vue 3 模板语
前端·javascript·vue.js
用户7610304473714 小时前
【转战智能眼镜开发】ROKID AIUI 小游戏 亚马逊标枪项目实战讲解
前端
风卿4 小时前
看着AI写代码太孤独了,来欣赏星云吧:一个DSH 装饰插件
前端·javascript
牧艺4 小时前
弹层不用再手算坐标了:CSS Anchor Positioning 从原理到可上线
前端·javascript·css
用户老李4 小时前
YOLO 目标检测自学教程 07:YOLOv3——多尺度预测
前端
moshuying5 小时前
ESP32 墨水屏金价牌(上):从点亮到把图片推过去
前端
李剑一5 小时前
别再抬杠,前端岗已经没了!无论前后端,我们最终都要成“全干”工程师
前端·面试