eventbus

作者: Pluto_7a23 | 来源:发表于2022-05-09 10:49 被阅读0次
    //利用ES6 的class构造函数
    class bus{
        constructor (){
            this.item = {} //传入接受的
        }
        $on(name,fu){
            // if(!this.item[name]){
            //     this.item[name] = []
            // }
            (this.item[name] ||= []).push(fu)
        }
        //
        $emit(name,...a){
            if(!this.item[name]) return;
            
            this.item[name].forEach(cd =>cd(a));
            console.log( this.item)
        }
        $off(name){
            this.item[name] = []
        }
    }
    
    

    相关文章

      网友评论

          本文标题:eventbus

          本文链接:https://www.haomeiwen.com/subject/iachurtx.html