vue3 子组件调用父组件的方法并返回结果

200 阅读1分钟

思路emit 事件传入callback

子组件

<script setup>
const emit = defineEmits(['submit']);

const handleSubmit =  () => {
    emit('submit', (val: boolean) => {
      if (!val) {
        return false;
      }
    });
}
<script/>

<template>
   <button  @click="handleSubmit">
    Click
   </button>
<template/>

父组件

<script setup>
const verification = (callback) => {
    callback(false)
}
<script/>

<template>
<ChildComponent @submit=(callback) => verification(callback) />
<template/>