VUE3组件 (2) 关于defineEmits()

本文详细介绍了在Vue3中如何使用defineProps和defineEmits进行父子组件间的通信。通过示例展示了如何在父组件传递数据到子组件,并通过子组件触发事件更新父组件的状态。同时,解释了defineEmits的用法,包括声明事件和传递参数。

前言

以下代码和内容的使用都是在setup中,未使用TS。

<script setup>
</script>

VUE3组件 (1) 关于defineProps()

VUE3组件 (3) 关于 slot 插槽

VUE3组件 (4) 关于 Provide Inject 依赖注入


使用defineEmits

单文件组件中。父子组件进行交互

1.例子

父级组:

<template>
  <section class="parent">
    <childVue :num="nums" @increase="handleIncrease"></childVue>
  </section>
</template>

<script setup>
  import childVue from './child.vue';
  import { ref } from 'vue';
  const nums = ref(0);
  const handleIncrease = () => {
    nums.value++;
  };
</script>

子组件:

<template>
  <section class="box" @click="handelClick">{{ num }}</section>
</template>

<script setup>
  const emits = defineEmits(['increase']);
  const handelClick = () => {
    emits('increase');
  };
</script>

页面表现为
在这里插入图片描述

2.说明

  1. 在父组件中传递了一个 num在子组件中展示。
  2. 父组件中
<childVue :num="nums" @increase="handleIncrease"></childVue>
// @increase 为监听事件 increase。handleIncrease为监听后执行的函数,这里是将 nums++
  1. 子组件中添加一个点击事件,方便后续的触发
<section class="box" @click="handelClick">{{ num }}</section>
// 给元素添加了 点击事件。去触发父级的 increase 监听。

3.用法 :

  1. 与defineProps一样,无需引入,直接可以使用。使用 defineEmits 声明事件(也就是父级页面上添加的 @监听事件)
const emits = defineEmits(['increase']);
 	 emits 为 defineEmits 显示声明后的对象。
 	 如存在多个监听事件则为 defineEmits(['increase','emit2','emit3'])
  const handelClick = () => {
    emits('increase');
    如果需要触发其他监听时间则为 emits('emit2');
    emits 对象触发的事件都应该为 defineEmits 声明后的事件
    
 };
  1. 传参
    在 emits() 的第一个参数,是监听事件的字面量。第二个参数为事件传递的参数。如果该事件有多个参数,第二个参数建议用对象的形式传递。
    示例
    //子组件发送
     emits('increase', {params1:'1',params2:'2'});
    //父组件监听
     const handleIncrease = (params) => {
    	console.log(' params1);//{params1:'1',params2:'2'}
    	nums.value += datas;
    };
    
### Vue3 中使用 `defineEmits` 进行父子组件事件传递 在 Vue 3 中,子组件可以利用 Composition API 提供的 `defineEmits` 函数定义可触发的自定义事件。父组件则可通过监听这些自定义事件来获取来自子组件的信息。 #### 子组件代码示例 ```javascript <script setup> import { ref } from &#39;vue&#39; // 定义 emit 可以触发的事件名 const emit = defineEmits([&#39;sendMessage&#39;]) let message = ref(&#39;Hello Parent&#39;) function sendMessageToParent() { // 调用 emit 并指定事件名为 sendMessage, 同时携带消息作为参数传出 emit(&#39;sendMessage&#39;, message.value) } </script> <template> <button @click="sendMessageToParent">Send Message to Parent</button> </template> ``` 此部分展示了如何设置子组件以便它可以发送信息给它的父级[^2]。 #### 父组件代码示例 ```html <!-- 父组件模板 --> <div id="app"> <!-- 监听子组件发出的消息,并将其绑定到 getMessageFromChild 方法上 --> <child-component @sendMessage="getMessageFromChild"></child-component> <p>Message received from child: {{message}}</p> </div> ``` ```javascript <script setup> import ChildComponent from &#39;./components/ChildComponent.vue&#39; import { ref } from &#39;vue&#39; let message = ref(&#39;No message yet.&#39;) function getMessageFromChild(data) { console.log(`Received data from child component: ${data}`) message.value = data; } </script> ``` 上述例子说明了当点击按钮后,子组件会调用 `emit()` 来通知父组件有新消息到达;而父组件已经绑定了相应的处理器函数 `getMessageFromChild` 处理接收到的数据[^1]。
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余九月丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值