26 lines
546 B
JavaScript
26 lines
546 B
JavaScript
// 简单的事件总线,用于组件间通信
|
|
class EventBus {
|
|
constructor() {
|
|
this.events = {};
|
|
}
|
|
|
|
on(event, callback) {
|
|
if (!this.events[event]) {
|
|
this.events[event] = [];
|
|
}
|
|
this.events[event].push(callback);
|
|
}
|
|
|
|
off(event, callback) {
|
|
if (!this.events[event]) return;
|
|
this.events[event] = this.events[event].filter(cb => cb !== callback);
|
|
}
|
|
|
|
emit(event, data) {
|
|
if (!this.events[event]) return;
|
|
this.events[event].forEach(callback => callback(data));
|
|
}
|
|
}
|
|
|
|
export default new EventBus();
|