chat-pc/src/hooks/useEventBus.ts

30 lines
535 B
TypeScript
Raw Normal View History

2024-12-24 08:14:21 +00:00
import { onMounted, onUnmounted } from 'vue'
import { bus } from '@/utils/event-bus'
interface Event {
name: any
event: Function
}
export const useEventBus = (items: Event[]) => {
if (!items.length) return
onMounted(() => {
for (const item of items) {
bus.subscribe(item.name, item.event)
}
})
onUnmounted(() => {
for (const item of items) {
bus.unsubscribe(item.name, item.event)
}
})
const emit = (channel: string, data: any) => {
bus.emit(channel, data)
}
return { emit }
}