vue3+ts通过ref访问组件里面的方法及属性

vue3+ts通过ref访问组件里面的方法及属性

之前访问不到主要是子组件没有往外暴露要访问的接口子组件使用了 script setup的组件是默认私有的:一个父组件无法访问到一个使用了 script setup的子组件中的任何东西,:
除非子组件在其中通过 defineExpose方法 宏显式暴露
子组件

复制代码
<template>
  <a-drawer v-model:open="open" class="custom-class" root-class-name="root-class-name" :root-style="{ color: 'blue' }"
    title="Basic Drawer" placement="right">
  </a-drawer>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const open = ref(false);
const showDrawer = () => {
  open.value = true;
};
defineExpose({
  showDrawer
})
</script>

父组件

复制代码
 <ApiModel ref="refApiModel"/>
import ApiModel from "./components/ApiModel.vue"
const refApiModel:any=ref(null)
const actionApiRecord=(type:string,record:any)=>{
  switch(type){
    case 'add':
        refApiModel.value?.showDrawer()
      break;
  }
}
相关推荐
超绝大帅哥12 分钟前
React的Fiber是什么? Vue为什么不需要Fiber ?
前端
yingyima18 分钟前
正则表达式分组与捕获:凌晨3点服务器报警的解决方案
前端
swipe1 小时前
从 0 到 1 理解 React 虚拟列表:定高、不定高与 Canvas 版本完整拆解
前端·javascript·面试
铁皮饭盒2 小时前
Bun执行python代码
前端·javascript·后端
hunterandroid2 小时前
Service 与前台服务:让任务在后台持续运行
前端
米饭同学i2 小时前
深扒 LobsterAI 官网前端动效实现方案:从交互细节到代码实践
前端
前端啊2 小时前
告别 el-table 打印难题,vue3-print-el-table 来了!
前端·vue.js
JarvanMo2 小时前
AI时代跨平台还有必要吗?
前端
Patrick_Wilson2 小时前
幂等到底是什么?从前端视角讲透 SQL、HTTP 与 POST 接口的幂等设计
前端·后端·架构
凌览3 小时前
一人公司别再上 Jenkins,真不值
前端·后端