yanjinhui
8 天以前 b9c76ce85e533250cd36de670146530f970859e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import mitt from 'mitt'
import { onBeforeUnmount } from 'vue'
 
interface Option {
  name: string // 事件名称
  callback: Fn // 回调
}
 
const emitter = mitt()
 
export const useEventBus = (option?: Option) => {
  if (option) {
    emitter.on(option.name, option.callback)
 
    onBeforeUnmount(() => {
      emitter.off(option.name)
    })
  }
 
  return {
    on: emitter.on,
    off: emitter.off,
    emit: emitter.emit,
    all: emitter.all
  }
}