class EventBus { constructor() { this._eventContainer = {}; } // 添加监听事件 on(type, fn) { this._eventContainer[type] = this._eventContainer[type] || []; this._eventContainer[type].push(fn); return this; } // 删除监听事件 off(type, fn) { if(this._eventContainer[type]) { let index = this._eventContainer[type].indexOf(fn); index >=0 && this._eventContainer[type].splice(index, 1); } return this; } // 触发监听事件 trigger(type, ...args) { if (type && this._eventContainer[type]) { [...this._eventContainer[type]].forEach((fn)=> { fn.apply(null, args); }) } return this; } // reset reset() { this._eventContainer = {}; } } const eventBus = new EventBus(); export default eventBus;