Skip to content

yuguaa/yuguaa-subscribe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Subscribe发布订阅类

使用import Subscribe from "@yugu/subscribe"导入类 通过const pool = new Subscribe创建发布订阅实例 该类实例可以通过add方式向订阅池中添加事件,pool.add(fn) 该类实例可以通过remove方式从订阅池中移除事件,pool.remove(fn) 该类实例可以通过fire方式触发订阅池中所有的函数,pool.fire()

<script type="module">
  import Subscribe from "@yugu/subscribe";
  const btn = document.querySelector(".submit");
  const pool = new Subscribe();
  const fn1 = () => {
    console.log(1);
  };
  const fn2 = () => {
    console.log(2);
    pool.remove(fn1);
  };
  const fn3 = () => {
    console.log(3);
  };
  const fn4 = () => {
    console.log(4);
  };
  pool.add(fn1);
  pool.add(fn2);
  pool.add(fn3);
  pool.add(fn4);
  btn.onclick = function (event) {
    pool.fire();
    console.log(pool);
  };
</script>

About

发布订阅

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published