JS 自定义事件通信

浏览器自定义事件通信示例

  window.addEventListener('hello',(e)=>{
      console.log(e.detail)
  });
  window.dispatchEvent(new CustomEvent('hello', {detail:{name:'张三'}}))

Node自定义事件通信示例

  const EventEmitter = require('events');
  const myEmitter = new EventEmitter();

  myEmitter.on('hello', (res) => {
    console.log(res);
  });
  
  myEmitter.emit('hello', {name:'张三'});
posted @ 2020-10-07 21:33  mingL  阅读(314)  评论(0编辑  收藏  举报