vue中父组件调用子组件方法
时间: 2023-11-03 21:53:39 浏览: 124
在Vue中,父组件可以通过使用ref
来获取子组件的实例,并调用子组件的方法。以下是一个简单的示例:
<template>
<div>
<ChildComponent ref="childComponent"></ChildComponent>
<button @click="callChildMethod">调用子组件方法</button>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent,
},
methods: {
callChildMethod() {
this.$refs.childComponent.childMethod();
},
},
};
</script>
在上面的示例中,父组件通过在ChildComponent
上添加ref
属性来引用子组件实例。然后,通过this.$refs.childComponent
可以访问子组件的实例,并调用子组件的方法childMethod()
。
请注意,为了能够使用ref
引用子组件,你需要确保在父组件中正确地声明了子组件,即在components
选项中注册了子组件。
相关推荐













