一文详细分析Vue3中的emit用法(子传父)

 更新时间:2024年05月28日 11:03:17   作者:码农研究僧  
Emit是Vue3中另一种常见的组件间传值方式,它通过在子组件中触发事件并将数据通过事件参数传递给父组件来实现数据传递,这篇文章主要给大家介绍了关于详细分析Vue3中emit用法(子传父)的相关资料,需要的朋友可以参考下

1. 基本知识

在 Vue 3 中,emit 是一种机制,用于在子组件中触发事件,并在父组件中监听这些事件

提供一种组件间通信的方式,尤其是在处理父子组件数据传递和交互时非常有用

一共有两种方式

1.1 emit

子组件中使用emit

<template>
  <button @click="handleClick">Click me</button>
</template>

<script>
export default {
  name: 'ChildComponent',
  methods: {
    handleClick() {
      this.$emit('custom-event', 'Hello from child');
    }
  }
}
</script>

父组件监听子组件:

<template>
  <div>
    <ChildComponent @custom-event="handleCustomEvent" />
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  methods: {
    handleCustomEvent(payload) {
      console.log(payload); // 输出 'Hello from child'
    }
  }
}
</script>

1.2 defineEmits

在 Vue 3 中,还可以使用 Composition API 的 defineEmits 方法来定义和使用 emit

子组件中定义和使用emit:

<template>
  <button @click="emitEvent">Click me</button>
</template>

<script setup>
import { defineEmits } from 'vue';

const emit = defineEmits(['custom-event']);

function emitEvent() {
  emit('custom-event', 'Hello from child with Composition API');
}
</script>

父组件监听子组件:

<template>
  <div>
    <ChildComponent @custom-event="handleCustomEvent" />
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  methods: {
    handleCustomEvent(payload) {
      console.log(payload); // 输出 'Hello from child with Composition API'
    }
  }
}
</script>

2. Demo

完整Demo如下:

  • 创建子组件:
<template>
  <button @click="emitEvent">Click me</button>
</template>

<script setup>
import { defineEmits } from 'vue';

const emit = defineEmits(['custom-event']);

function emitEvent() {
  emit('custom-event', 'Hello from child with Composition API');
}
</script>
  • 创建父组件:
<template>
  <div>
    <ChildComponent @custom-event="handleCustomEvent" />
    <p>{{ message }}</p>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';
import { ref } from 'vue';

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  setup() {
    const message = ref('');

    function handleCustomEvent(payload) {
      message.value = payload;
    }

    return {
      message,
      handleCustomEvent
    };
  }
}
</script>
  • 应用组件:
<template>
  <ParentComponent />
</template>

<script>
import ParentComponent from './components/ParentComponent.vue';

export default {
  name: 'App',
  components: {
    ParentComponent
  }
}
</script>

主入口文件:

import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');

总结 

到此这篇关于Vue3中的emit用法(子传父)的文章就介绍到这了,更多相关Vue3中emit用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 细说Vue组件的服务器端渲染的过程

    细说Vue组件的服务器端渲染的过程

    这篇文章主要介绍了细说 Vue 组件的服务器端渲染,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • element中datepicker日期选择器选择周一到周日并实现上一周和下一周的方法

    element中datepicker日期选择器选择周一到周日并实现上一周和下一周的方法

    最近项目中需要用到日期选择器,所以这里给大家总结下,这篇文章主要给大家介绍了关于element中datepicker日期选择器选择周一到周日并实现上一周和下一周的相关资料,需要的朋友可以参考下
    2023-09-09
  • 详解Nuxt.js Vue服务端渲染摸索

    详解Nuxt.js Vue服务端渲染摸索

    本篇文章主要介绍了详解Nuxt.js Vue服务端渲染摸索,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • ElementUI之Message功能拓展详解

    ElementUI之Message功能拓展详解

    这篇文章主要介绍了ElementUI之Message功能拓展详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • vue实现图片滑动验证功能

    vue实现图片滑动验证功能

    这篇文章主要为大家详细介绍了vue实现图片滑动验证功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Vue2+Elementui Dialog实现封装自定义弹窗组件

    Vue2+Elementui Dialog实现封装自定义弹窗组件

    在日常的管理系统界面中,我们写的最多的除了列表表格之外,就是各种弹窗组件,本文就来为大家详细介绍一下Vue2如何结合Elementui Dialog实现封装自定义弹窗组件,希望对大家有所帮助
    2023-12-12
  • vue package.json文件的使用及说明

    vue package.json文件的使用及说明

    `package.json`是Node.js和npm项目的核心配置文件,定义了项目的元数据和依赖项,在Vue项目中,它管理依赖、提供脚本命令、存储项目信息,并通过语义化版本控制确保项目稳定
    2025-01-01
  • vue监视器@Watch创建执行immediate方式

    vue监视器@Watch创建执行immediate方式

    这篇文章主要介绍了vue监视器@Watch创建执行immediate方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • vue创建项目卡住不动,vue create project卡住不动的解决

    vue创建项目卡住不动,vue create project卡住不动的解决

    这篇文章主要介绍了vue创建项目卡住不动,vue create project卡住不动的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue cli2 和 cli3去掉eslint检查器报错的解决

    vue cli2 和 cli3去掉eslint检查器报错的解决

    这篇文章主要介绍了vue cli2 和 cli3去掉eslint检查器报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04

最新评论