有一個帶有像這樣的 ref 的組件:
<template>
<custom-component
ref="func"
/>
</template>
<script setup>
const func = ref();
</script>
在組件內部有一個這樣的函式:
const helloWorld = () => {
console.log('hello World');
}
如何從父組件訪問 helloWorld 函式?
uj5u.com熱心網友回復:
假設您的子組件也在使用<script setup>,則組件的定義默認關閉(不公開)。
您可以defineExpose在子組件中手動公開該方法:
// CustomComponent.vue
<script setup>
const helloWorld = /*...*/
defineExpose({
helloWorld
})
</script>
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383527.html
