vue3中使用emit

前言

最近在学习vue3的东西,需要从子组件传递内容到父组件,了解到了emit,和v2稍微有点区别,所以下面记录下

学习

父组件中

①定义函数
②使用子组件时使用该函数

1
2
3
const fatherFn = (参数:接收子组件传递的数据) =>{
函数体...
}
1
<Child @fatherFn ="fatherFn" />

子组件

①子组件引入defineEmits
②赋值给emit
③触发

1
2
3
4
import {  defineEmits} from 'vue';
const emit = defineEmits(['fatherFn']);
//触发
emit('fatherFn ', {参数:传递给父组件的数据});

vue3中去掉了this,所以稍微和vue2有点区别,我使用<script setup>这种语法