vue3获取子组件ref结果undefined,父组件调用子组件的方法

需求是在切换货号的时候,清空上传过的图片,upload是子组件,在父组件的事件中调用子组件的方法


用了let uploadRef= ref(null);获取不到,用 proxy.$refs['uploadRef']才能获取到实例

import {  getCurrentInstance } from "vue"
const { proxy } = getCurrentInstance();
const getNewData =  () => {
  proxy.$refs['uploadRef'].clearImg();
}

另外如果子组件用了setup语法糖,记得要把方法defineExpose出来,,setup语法糖是封闭的,不需要写return,所以如果子组件方法要去给别人用的时候,就要用defineExpose 来暴露

const clearImg = () => {
  fileList.value = [];
};
defineExpose({ clearImg });

你可能感兴趣的:(vue3获取子组件ref结果undefined,父组件调用子组件的方法)