github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/hooks/event/useEventBus.ts (about) 1 import mitt from 'mitt' 2 import { onBeforeUnmount } from 'vue' 3 4 interface Option { 5 name: string // 事件名称 6 callback: Fn // 回调 7 } 8 9 const emitter = mitt() 10 11 export const useEventBus = (option?: Option) => { 12 if (option) { 13 emitter.on(option.name, option.callback) 14 15 onBeforeUnmount(() => { 16 emitter.off(option.name) 17 }) 18 } 19 20 return { 21 on: emitter.on, 22 off: emitter.off, 23 emit: emitter.emit, 24 all: emitter.all 25 } 26 }