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;
  }
}
相关推荐
用户0595401744611 分钟前
把Agent记忆测试从Mock换到真实Redis,漏测率从30%降到0
前端·css
吃阿茶搽12 分钟前
大模型RAG实战,从被骂不靠谱到成为部门MVP,我的踩坑全记录
vue.js
Surprisec12 分钟前
如何用 TypeScript 写一个最小可运行的 CLI Agent
前端·人工智能·typescript
marskim13 分钟前
零依赖、高性能!从零实现 React 拖拽排序组件(基于 HTML5 Drag and Drop API)
前端
jingling55515 分钟前
从零到一:用 Aholo Viewer 在浏览器里渲染 3D 高斯泼溅小熊
linux·前端·ubuntu·3d
情多多7717 分钟前
基于NetCorePal Cloud Framework的DDD架构管理系统实践
javascript
Good kid.22 分钟前
开源一套 Vue3 多模态 AI 控制台前端:游乐场、工作室与 API 文档页
前端·人工智能·开源
小林ixn23 分钟前
前后端模块化分离实战:从零搭建用户列表展示(HTML+CSS+JS + json-server)
前端·javascript
天才熊猫君23 分钟前
我把一个 bug 发给 ai,ai 直接通过 playwright mcp 直接排查出 bug。。
前端·javascript
meilindehuzi_a32 分钟前
打破0基础:通过 5 个核心案例深度拆解 JavaScript 正则表达式与运行时类型系统
开发语言·javascript·正则表达式