我有一个父组件,我需要调用其子组件之一中存在的方法:
<template>
<div>
<button @click="callChildMethod">
<child-component ref="child" />
</div>
</template>
<script>
setup(props) {
const child = ref(null)
const callChildMethod = () => {
child.doSomething()
}
return {
child,
callChildMethod
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
子组件包含doSomething方法:
const doSomething = () => { console.log('calling method....') }
Run Code Online (Sandbox Code Playgroud)
由于我使用的是 VueJS3 和 Composition API,因此我的方法是使用模板引用来调用子组件中的方法。显然不起作用,但我看不出我错过了什么。有人对这个有线索吗?提前致谢
javascript vue.js vuejs3 vue-composition-api vue-script-setup