vue父組件調用子組件的方法,獲取同步(實時)的返回結果
//父組件 <template> <Child ref="childRef"> </template> <script> import Child from '.Child.vue' components:{ Child, } setup(props,ctx){ const childRef=ref()
//調用子組件的方法 const getChildValue=()=>{ childRef.value.handlerSelectConfirm().then(selectData=>{ //得到返回值 }) } return { childRef, getChildValue } } </script>
子組件
//子組件 <template> </template> <script> setup(props,ctx){ const chooseData= ref([]) const handlerSelectConfirm=async()=>{ return new Promise((resolve,reject)=>{ //返回結果 resolve(chooseData.value) }) } return { chooseData, handlerSelectConfirm, } </script>