vue3兄弟组件传参bus

Bus.ts

 
type BusClass = {
    emit: (name: string) => void
    on: (name: string, callback: Function) => void
}
type BusParams = string | number | symbol 
type List = {
    [key: BusParams]: Array
}
class Bus  implements BusClass {
    list: List
    constructor() {
        this.list = {}
    }
    emit(name: string, ...args: Array) {
        let eventName: Array = this.list[name]
        eventName.forEach(ev => {
            ev.apply(this, args)
        })
    }
    on(name: string, callback: Function) {
        let fn: Array = this.list[name] || [];
        fn.push(callback)
        this.list[name] = fn
    }
}
 
export default new Bus()

使用a组件





b组件





你可能感兴趣的:(vue3兄弟组件传参bus)