vue3子组件向父组件传递方法
时间: 2023-06-21 16:09:47 浏览: 58
在 Vue3 中,可以使用 `emit` 方法从子组件向父组件传递方法。具体步骤如下:
1. 在子组件中定义一个方法,并在该方法中通过 `emit` 方法将该方法传递给父组件。
```vue
<template>
<button @click="handleClick">点击</button>
</template>
<script>
export default {
methods: {
handleClick() {
this.$emit('childClick')
}
}
}
</script>
```
2. 在父组件中通过 `v-on` 或 `@` 指令监听子组件传递的方法。
```vue
<template>
<child-component @childClick="handleChildClick"></child-component>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
components: {
ChildComponent
},
methods: {
handleChildClick() {
console.log('子组件传递的方法')
}
}
}
</script>
```
在子组件中使用 `$emit` 方法将方法传递给父组件时,可以传递任意数量的参数。在父组件中监听子组件传递的方法时,也可以接收这些参数。