我有一个父组件,我需要调用它的一个子组件中存在的方法:
<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>
子组件包含doSomething
方法:
const doSomething = () => { console.log('calling method....') }
因为我使用的是VueJS3和Composition,所以我的方法是使用template ref来调用子组件中的方法。显然不起作用,但我看不到我错过了什么。有没有人知道这件事?提前感谢
发布于 2020-11-18 09:01:43
您的ref
中缺少value字段,它应该是:
const callChildMethod = () = {
child.value.doSomething()
}
https://stackoverflow.com/questions/64889756
复制