mirror of
https://gitee.com/myxzgzs/boyue-vue-pro.git
synced 2025-08-09 08:52:44 +08:00
24 lines
374 B
TypeScript
24 lines
374 B
TypeScript
![]() |
import mitt from 'mitt'
|
||
|
import { onBeforeUnmount } from 'vue'
|
||
|
|
||
|
interface Option {
|
||
|
name: string // 事件名称
|
||
|
callback: Fn // 回调
|
||
|
}
|
||
|
|
||
|
const emitter = mitt()
|
||
|
|
||
|
export const useEmitt = (option?: Option) => {
|
||
|
if (option) {
|
||
|
emitter.on(option.name, option.callback)
|
||
|
|
||
|
onBeforeUnmount(() => {
|
||
|
emitter.off(option.name)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
emitter
|
||
|
}
|
||
|
}
|