Vue3 (four) pass the method of the child component to the parent component through the ref object

There is such a business scenario, when the save button is clicked, the data of the sub-form is obtained and submitted to the background.
In vue2, just this.$refs.formObj.getData() can be used directly.

After vue3, writing like this will not work. Du Niang will only know after reading it. It needs to be declared through defineExpose.

/**
 * 返回接口操作
 */
const getData= () => {
   return state.domainData;
}
defineExpose({getData});

Can only be called after declaration

 const panelForm = ref(null);
 //获取数据
let data=(panelForm.value as any).getData();

Guess you like

Origin blog.csdn.net/weiyongliang_813/article/details/126736833