提交 | 用户 | 时间 | ||
820397 | 1 | import mitt from 'mitt' |
H | 2 | |
3 | interface Option { | |
4 | name: string // 事件名称 | |
5 | callback: Fn // 回调 | |
6 | } | |
7 | ||
8 | const emitter = mitt() | |
9 | ||
10 | export const useEmitt = (option?: Option) => { | |
11 | if (option) { | |
12 | emitter.on(option.name, option.callback) | |
13 | ||
14 | onBeforeUnmount(() => { | |
15 | emitter.off(option.name) | |
16 | }) | |
17 | } | |
18 | ||
19 | return { | |
20 | emitter | |
21 | } | |
22 | } |