【vue3】父组件\子组件 defineExpose

105 阅读1分钟

defineExpose

// 子组件
<script setup>
import { ref defineExpose } from 'vue'
const openChild = () => {
 console.log('点击子组件')
}
//将方法暴露出
defineExpose({openChild})
</script>

// 父组件
<template>
    <button @click="open">按钮</button>
    <Child ref="child" />
</template>

<script setup>

const child = ref()
const open = () => {
    child.value.openChild()
}
</script>