news 2026/4/3 4:18:13

Vue 3 中使用 Mitt 事件总线

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue 3 中使用 Mitt 事件总线

Vue3中使用Mitt事件总线实现组件通信。


Mitt是一个轻量级的事件库,可作为全局事件总线替代方案。


文章介绍了基本使用方法:创建事件总线实例、定义事件类型、在组件中发射和监听事件,并强调组件卸载时需取消监听以避免内存泄漏。


还展示了高级用法:工厂模式创建多总线、封装Vue插件、CompositionAPI封装Hook。


最后对比了Mitt与Vue原生通信方式的差异,建议谨慎使用事件总线,优先考虑props/emit或provide/inject等更明确的通信方式。


Vue 3 中使用 Mitt 事件总线

Mitt 是一个轻量级的事件发射/监听库,在 Vue 3 中可以作为全局事件总线的替代方案。

安装

npm install mitt # 或 yarn add mitt # 或 pnpm add mitt

基本使用

1.创建事件总线实例

方式一:创建全局事件总线

// src/utils/eventBus.ts import mitt from 'mitt' type Events = { // 定义事件类型 'user-login': { userId: string; username: string } 'user-logout': void 'notification': string 'update-count': number // 使用通配符监听所有事件 '*': { type: string; payload?: any } } const eventBus = mitt<Events>() export default eventBus

方式二:在 Composition API 中使用

vue

<!-- ComponentA.vue --> <script setup lang="ts"> import { onUnmounted } from 'vue' import eventBus from '@/utils/eventBus' // 发射事件 const emitLogin = () => { eventBus.emit('user-login', { userId: '123', username: '张三' }) } // 监听事件 eventBus.on('notification', (message) => { console.log('收到通知:', message) }) // 监听所有事件 eventBus.on('*', (type, payload) => { console.log(`事件类型: ${type}`, payload) }) // 组件卸载时取消监听 onUnmounted(() => { eventBus.off('notification') // 或者取消所有监听 // eventBus.all.clear() }) </script>

2.在多个组件中使用

vue

<!-- Header.vue --> <script setup lang="ts"> import eventBus from '@/utils/eventBus' const logout = () => { eventBus.emit('user-logout') } const sendNotification = () => { eventBus.emit('notification', '新消息!') } </script>

vue

<!-- Sidebar.vue --> <script setup lang="ts"> import { ref, onMounted, onUnmounted } from 'vue' import eventBus from '@/utils/eventBus' const notification = ref('') const handleNotification = (message: string) => { notification.value = message setTimeout(() => notification.value = '', 3000) } onMounted(() => { eventBus.on('notification', handleNotification) }) onUnmounted(() => { eventBus.off('notification', handleNotification) }) </script>

高级用法

1.使用工厂模式创建多个事件总线

// src/utils/eventBusFactory.ts import mitt from 'mitt' export function createEventBus<T>() { return mitt<T>() } // 创建不同的事件总线 export const uiEventBus = createEventBus<{ 'modal-open': { id: string } 'modal-close': string }>() export const dataEventBus = createEventBus<{ 'data-loaded': any[] 'data-error': Error }>()

2.封装为 Vue 插件

// src/plugins/eventBus.ts import { type App } from 'vue' import mitt, { type Emitter } from 'mitt' // 事件类型定义 type Events = { [key: string]: any } // 创建全局事件总线 const eventBus: Emitter<Events> = mitt<Events>() export const EventBusPlugin = { install(app: App) { // 全局属性 app.config.globalProperties.$eventBus = eventBus // 提供/注入 app.provide('eventBus', eventBus) } } // 在 Composition API 中使用的 Hook export function useEventBus() { const eventBus = inject<Emitter<Events>>('eventBus') if (!eventBus) { throw new Error('Event bus not provided') } return eventBus } export default eventBus

main.ts

// main.ts import { createApp } from 'vue' import { EventBusPlugin } from '@/plugins/eventBus' import App from './App.vue' const app = createApp(App) app.use(EventBusPlugin) app.mount('#app')

3.在 Composition API 中封装 Hook

typescript

// src/composables/useEventBus.ts import { onUnmounted } from 'vue' import eventBus, { type Handler } from '@/utils/eventBus' export function useEventBus() { const listeners: Array<[string, Handler]> = [] const emit = <T = any>(event: string, payload?: T) => { eventBus.emit(event, payload) } const on = <T = any>(event: string, handler: (payload: T) => void) => { eventBus.on(event, handler as Handler) listeners.push([event, handler as Handler]) } const off = <T = any>(event: string, handler: (payload: T) => void) => { eventBus.off(event, handler as Handler) const index = listeners.findIndex( ([e, h]) => e === event && h === handler ) if (index > -1) { listeners.splice(index, 1) } } const once = <T = any>(event: string, handler: (payload: T) => void) => { const onceHandler = (payload: T) => { handler(payload) off(event, onceHandler) } on(event, onceHandler) } // 自动清理监听器 onUnmounted(() => { listeners.forEach(([event, handler]) => { eventBus.off(event, handler) }) listeners.length = 0 }) return { emit, on, off, once } }

vue

<!-- 使用封装的 Hook --> <script setup lang="ts"> import { useEventBus } from '@/composables/useEventBus' const { emit, on, once } = useEventBus() // 发送事件 const sendEvent = () => { emit('custom-event', { data: 'test' }) } // 监听事件 on('custom-event', (payload) => { console.log('收到事件:', payload) }) // 只监听一次 once('one-time-event', (payload) => { console.log('只会触发一次:', payload) }) </script>

与 Vue 原生方法的比较

特性MittVue 3 的emit/propsProvide/Inject
通信范围任意组件间父子组件间祖先-后代组件间
类型支持TypeScript 友好TypeScript 友好TypeScript 友好
耦合度
适用场景全局事件、兄弟组件父子组件层级深的组件

最佳实践

  1. 类型安全

    // 正确定义事件类型 type Events = { 'user-updated': User 'cart-changed': CartItem[] }
  2. 及时清理

    // 组件卸载时取消监听 onUnmounted(() => { eventBus.off('event-name', handler) })
  3. 避免过度使用

    • 优先使用 props/emit 进行父子组件通信

    • 优先使用 provide/inject 进行层级通信

    • 只在需要跨多级/兄弟组件通信时使用事件总线

  4. 错误处理

    const { emit, on } = useEventBus() on('error-event', (error) => { // 统一错误处理 console.error('事件错误:', error) })

注意事项

  1. 内存泄漏:务必在组件卸载时取消事件监听

  2. 调试困难:事件总线可能导致数据流不清晰

  3. 替代方案对于复杂应用,考虑使用 Pinia 进行状态管理


Mitt 在 Vue 3 中是一个简单有效的跨组件通信方案,但应谨慎使用,避免滥用导致代码难以维护。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/15 18:16:17

NewBie-image-Exp0.1开箱即用:一键生成高质量动漫图片

NewBie-image-Exp0.1开箱即用&#xff1a;一键生成高质量动漫图片 你是否曾为部署一个动漫图像生成模型而烦恼&#xff1f;环境配置复杂、依赖冲突频发、源码Bug层出不穷……这些问题常常让刚入门AI绘画的开发者望而却步。今天&#xff0c;我们带来一款真正“开箱即用”的解决…

作者头像 李华
网站建设 2026/3/9 12:16:36

亲测verl框架:在老旧P40显卡上实现RLHF训练体验

亲测verl框架&#xff1a;在老旧P40显卡上实现RLHF训练体验 强化学习人类反馈&#xff08;RLHF&#xff09;是当前大模型对齐技术的核心环节&#xff0c;但其训练通常依赖高端GPU集群。作为一名预算有限的开发者&#xff0c;我手头只有一块2016年发布的Tesla P40&#xff08;2…

作者头像 李华
网站建设 2026/3/28 10:57:10

DeepSeek-R1-Distill-Qwen-1.5B高效运行:Python 3.11+兼容性实测

DeepSeek-R1-Distill-Qwen-1.5B高效运行&#xff1a;Python 3.11兼容性实测 你是否也在寻找一个轻量但推理能力出色的中文大模型&#xff1f;最近我入手了 DeepSeek-R1-Distill-Qwen-1.5B&#xff0c;这个基于强化学习蒸馏技术打造的15亿参数模型&#xff0c;在数学、代码和逻…

作者头像 李华
网站建设 2026/3/28 5:52:04

一键启动Z-Image-Turbo_UI界面,轻松实现AI图像生成

一键启动Z-Image-Turbo_UI界面&#xff0c;轻松实现AI图像生成 你是否也曾经被那些复杂的AI模型部署流程劝退&#xff1f;下载模型、配置环境、安装依赖、写代码调用……每一步都像在闯关。今天要介绍的 Z-Image-Turbo_UI界面&#xff0c;就是来帮你“一键通关”的。 只需一条…

作者头像 李华
网站建设 2026/3/28 13:01:43

零配置部署Qwen3-0.6B,新手友好指南

零配置部署Qwen3-0.6B&#xff0c;新手友好指南 你是不是也经常被各种复杂的模型部署流程劝退&#xff1f;下载权重、配置环境、安装依赖、启动服务……一连串操作下来&#xff0c;还没开始用模型就已经累得不想继续了。今天这篇文章就是为你准备的——我们来彻底简化这个过程…

作者头像 李华