首页 > 其他分享 >兄弟组件传参_bus封装

兄弟组件传参_bus封装

时间:2022-11-02 20:49:04浏览次数:133  
标签:传参 封装 name bus .# list fn string

vue3 Bus兄弟组件传参

type BusClass = {
  emit: (name: string) => void
  on: (name: string, callBack: Function) => void
}

type ParamsKey = string | number | symbol

type List = {
  [key: ParamsKey]: Array<Function>
}

class Bus implements BusClass {
  #list: List
  constructor() {
    this.#list = {}
  }

  emit(name: string, ...args: Array<any>) {
    const eventName: Array<Function> = this.#list[name]
    eventName.forEach(fn => {
      fn.apply(this, args)
    })
  }
  on(name: string, callBack: Function) {
    const fn = this.#list[name] || []
    fn.push(callBack)
    this.#list[name] = fn
  }
}

export default new Bus()

Mitt库

https://www.bilibili.com/read/cv16107098/

标签:传参,封装,name,bus,.#,list,fn,string
From: https://www.cnblogs.com/JunLan/p/16852341.html

相关文章