111
This commit is contained in:
34
Web/src/composables/useEventBus.ts
Normal file
34
Web/src/composables/useEventBus.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { EventBusHandler } from '~/extends/EventBusExtend'
|
||||
|
||||
/**
|
||||
* 事件总线组合式封装
|
||||
* 在组件作用域内订阅时会自动解绑
|
||||
*/
|
||||
export const useEventBus = () => {
|
||||
const registerDispose = (unsubscribe: () => void) => {
|
||||
if (getCurrentScope()) {
|
||||
onScopeDispose(unsubscribe)
|
||||
}
|
||||
|
||||
return unsubscribe
|
||||
}
|
||||
|
||||
const on = <T = unknown>(event: string, handler: EventBusHandler<T>) => {
|
||||
return registerDispose(EventBusExtend.on(event, handler))
|
||||
}
|
||||
|
||||
const off = <T = unknown>(event: string, handler?: EventBusHandler<T>) => {
|
||||
EventBusExtend.off(event, handler)
|
||||
}
|
||||
|
||||
const emit = <T = unknown>(event: string, payload?: T) => {
|
||||
EventBusExtend.emit(event, payload)
|
||||
}
|
||||
|
||||
return {
|
||||
on,
|
||||
off,
|
||||
emit,
|
||||
clear: EventBusExtend.clear
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user