zoukankan      html  css  js  c++  java
  • wather.js 封装

    /** @format */
    /* eslint-disable @typescript-eslint/no-explicit-any */
    /* eslint-disable @typescript-eslint/explicit-module-boundary-types */

    // 观察者
    class Watcher {
        public list: any;
        constructor() {
          this.list = {};
        }
        // 订阅
        public $on(key: string, fn: any): void {
          if (!this.list[key]) {
            this.list[key] = [];
          }
          this.list[key].push(fn);
        }
        // 发布
        public $emit(key: string, param?: any): void | boolean {
          const fns = this.list[key];
          // 检测是否有注册  注册后是否有注入方法
          if (!fns || fns.length === 0) {
            return false;
          }
          // 依次执行 并传入参数
          fns.forEach((fn: any) => {
            fn(param);
          });
        }
        // 清空
        public $clear(key: string) {
          this.list[key] = [];
        }
        public $clearAll() {
          this.list = {};
        }
      }
      
      export default new Watcher();  
  • 相关阅读:
    电脑分屏鼠标移不到另一个电脑上
    jquery datetimepicker 配置参数
    js获取前n天或者后n天的天数
    mysql查看变量
    Underscore _.template 方法使用详解
    sql优化
    echart图表demo
    jquery预加载的几种例子
    树莓派搭建服务器
    Thymeleaf标签学习
  • 原文地址:https://www.cnblogs.com/wenqylh/p/14964946.html
Copyright © 2011-2022 走看看