Vue 非父子组件可以用 Event Bus 通信,用起来很简单但要不只是会使用 api,还要懂大致原理,于是自己实现了一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
   | class EventBus {   constructor() {     this._events = new Map()   }
       on(name, fn) {     const handler = this._events.get(name)     if (!handler) {       this._events.set(name, [])     }     this._events.get(name).push(fn)   }
    
 
 
 
    emit(name, ...args) {     const handler = this._events.get(name)     if (!handler) return     handler.forEach(cb => {       if (args.length >= 3) {         cb.apply(this, args)       } else {         cb.call(this, ...args)       }     })   }
       once(name, fn) {     const cb = (...args) => {       if (args.length >= 3) {         fn.apply(this, args)       } else {         fn.call(this, ...args)       }       this.off(name, cb)     }     this.on(name, cb)   }
       off(name, fn) {     if (typeof fn === 'undefined') {              this._events.delete(name)       return     }     const handler = this._events.get(name)     if (!handler) return     let fnIndex = handler.findIndex(cb => cb === fn)     handler.splice(fnIndex, 1)     if (!this._events.get(name).length) {       this._events.delete(name)     }   } }
  |