vue3获取子组件实例

 方法一,直接获取

// 子组件 抛出方法
defineExpose({ init });
// 父组件
// 1.引入子组件
import addMinorJnjuries from "../dialog/minorJnjuriesDialog/addMinorJnjuries.vue";
      <addMinorJnjuries
        ref="addRef"
        :dialogVisible="showDialog"
        @close="closeDialog"
        @getDataList="getDataList"
      ></addMinorJnjuries>
      
// 2.获取子组件ref实例
const addRef = ref<InstanceType<typeof addMinorJnjuries>>();
// 3.使用
if (addRef.value) addRef.value.init(row.id, "编辑");

// 注意   不加if判断会有警告 !!!

方法二 在父组件实例中获取

getCurrentInstance

import { getCurrentInstance } from "vue";

const { proxy } = <any>getCurrentInstance();

猜你喜欢

转载自blog.csdn.net/m0_59338367/article/details/128293309