vue父组件如何把方法传给子组件
时间: 2024-02-28 15:50:37 浏览: 19
在Vue中,父组件可以通过props属性将方法传递给子组件。下面是一个简单的示例:
父组件:
```html
<template>
<div>
<ChildComponent :childMethod="parentMethod" />
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent
},
methods: {
parentMethod() {
// 父组件的方法逻辑
}
}
}
</script>
```
子组件:
```html
<template>
<div>
<button @click="childMethod">调用父组件方法</button>
</div>
</template>
<script>
export default {
props: ['childMethod'],
methods: {
childMethod() {
this.childMethod(); // 调用父组件传递的方法
}
}
}
</script>
```
在父组件中,使用`ChildComponent`标签,并通过`childMethod`属性将父组件的方法传递给子组件。在子组件中,使用`props`接收父组件传递的方法,并在需要的地方调用该方法。