zoukankan      html  css  js  c++  java
  • 如何封装$on,$emit,$off——学vue前你必须懂得封装!

    let evevtListenr = {}
    封装$on
    const $on = (eventName,cb)=>{
        if(!evevtListenr[eventName]){
            evevtListenr[eventName] =[];
        }
        evevtListenr[eventName].push(cb);
        }
        封装$emit
    const $emit = (eventName,params)=>{
            if(evevtListenr[eventName]){
                evevtListenr[eventName].map((cb)=>{
                    cb(params);
                })
            }
        }
    封装$off
        const $off = (eventName,cb)=>{
            if(evevtListenr[eventName]){
                if(cb){
                let index=vevtListenr[eventName].indexof(cb);
                evevtListenr[eventName].splice(index,1);
                }else{
                    evevtListenr[eventName].length=0;
                }
            }
        }
        
        function fn1(val){
            console.log(111,val);
        };
        function fn2(val){
            console.log(222,val);
        };
        function fn3(val){
            console.log(333,val);
        }
        $on('work1',fn1);
        $on('work1',fn2);
        $on('work1',fn3);
        $emit('work1');
        
  • 相关阅读:
    Codeforces 812E Sagheer and Apple Tree
    bzoj 4765: 普通计算姬
    bzoj 4552: [Tjoi2016&Heoi2016]排序
    bzoj 1096: [ZJOI2007]仓库建设
    bzoj 1030: [JSOI2007]文本生成器
    bzoj 1095: [ZJOI2007]Hide 捉迷藏
    JS实现HashMap
    A4纸表格打印
    JAVA字符串格式化-String.format()的使用
    证书打印CSS知识点总结
  • 原文地址:https://www.cnblogs.com/robot666/p/11111503.html
Copyright © 2011-2022 走看看