vue3组件通信之defineEmits

一、defineEmits是什么?

defineEmits 是vue3提供的方法,又称为自定义事件,不需要引入可以直接使用,用于子组件与父组件通信。

二、使用样例

1.父组件代码

代码如下(示例):

<template>
  <div>
    <h1>事件</h1>
    <!-- vue3:原生的DOM事件不管是放在标签身上、组件标签身上都是原生DOM事件-->
    <!-- 绑定自定义事件xxx:实现子组件给父组件传递数据 -->
    <Event2 @xxx="handler3" @click="handler4"></Event2>
  </div>
</template>

<script setup lang="ts">
//引入子组件
import Event2 from './Event2.vue';
const handler3 = (param1: any,param2: any)=>{
    console.log(param1,param2);
}
//事件回调--5
const handler4 = (param1: any,param2: any)=>{
     console.log(param1,param2);
}
</script>

<style scoped>
</style>

2.子组件代码

代码如下(示例):

<template>
  <div class="child">
    <p>我是子组件2</p>
    <button @click="handler">点击我触发自定义事件xxx</button>
    <button @click="$emit('click','AK47','J20')">点击我触发自定义事件click</button>
  </div>
</template>

<script setup lang="ts">
//利用defineEmits方法返回函数触发自定义事件
//defineEmits方法不需要引入直接使用
let $emit = defineEmits(['xxx','click']);
//按钮点击回调
const handler = () => {
  //第一个参数:事件类型 第二个|三个|N参数即为注入数据
    $emit('xxx','东风导弹','航母');
};
</script>

<style scoped>
.child {
  width: 400px;
  height: 200px;
  background: pink;
}
</style>

总结

vue3:原生的DOM事件不管是放在标签身上、组件标签身上都是原生DOM事件,利用defineEmits方法返回函数触发自定义事件,defineEmits方法不需要引入直接使用。

Vue3的组件通信方式和Vue2有些不同,主要是因为Vue3中使用了Composition API,其中提供了两种方式进行组件通信: 1. Props 和 Emit 这是Vue2中也存在的方式,通过在父组件中向子组件传递props,子组件通过emit事件向父组件传递信息。在Vue3中,可以使用 `defineProps` 和 `defineEmits` 来定义props和emit事件。示例: ```html <!-- Parent.vue --> <template> <Child :msg="message" @send-msg="handleMsg"></Child> </template> <script> import { defineComponent, ref } from 'vue' import Child from './Child.vue' export default defineComponent({ components: { Child }, setup() { const message = ref('Hello') const handleMsg = (msg) => { console.log(`Received message from child: ${msg}`) } return { message, handleMsg } } }) </script> <!-- Child.vue --> <template> <button @click="sendMsg">Send Message</button> </template> <script> import { defineComponent, defineEmits, propType } from 'vue' export default defineComponent({ emits: ['send-msg'], props: { msg: { type: String, required: true } }, setup(props, { emit }) { const sendMsg = () => { emit('send-msg', 'Hello from child') } return { sendMsg } } }) </script> ``` 2. Provide 和 Inject Provide和Inject是Vue3中新增的方式,可以在父组件中提供数据,让子组件中使用。Provide和Inject可以传递任何类型的数据,包括对象、数组、函数等。示例: ```html <!-- Parent.vue --> <template> <div> <h1>{{ title }}</h1> <Child /> </div> </template> <script> import { defineComponent, provide } from 'vue' import Child from './Child.vue' export default defineComponent({ components: { Child }, setup() { provide('title', 'Hello from parent') } }) </script> <!-- Child.vue --> <template> <div> <h2>{{ subtitle }}</h2> </div> </template> <script> import { defineComponent, inject } from 'vue' export default defineComponent({ setup() { const title = inject('title', 'Default title') const subtitle = 'Hello from child' return { title, subtitle } } }) </script> ``` 以上是两种Vue3中常用的组件通信方式,具体使用哪种方式取决于组件之间的关系和需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值