vue3中使用emit
前言
最近在学习vue3的东西,需要从子组件传递内容到父组件,了解到了emit,和v2稍微有点区别,所以下面记录下
学习
父组件中
①定义函数
②使用子组件时使用该函数
1 | const fatherFn = (参数:接收子组件传递的数据) =>{ |
1 | <Child @fatherFn ="fatherFn" /> |
子组件
①子组件引入defineEmits
②赋值给emit
③触发
1 | import { defineEmits} from 'vue'; |
vue3中去掉了this,所以稍微和vue2有点区别,我使用<script setup>
这种语法