向JavaScript Event添加有效负载



是否有一种方法可以像在jQuery中那样添加有效载荷到香草JavaScript事件?

// jQuery
$(document).trigger('event', payload);
// Vanilla JS
window.dispatchEvent(new Event('event', ???

我想我必须使用我自己的某种事件总线?

尝试使用CustomEvent,参见创建和触发事件

function eventHandler(e) {
  console.log("detail:", e.detail);
}
window.addEventListener("build", eventHandler);
window.dispatchEvent(new CustomEvent("build", {"detail": {"abc":123}}));

相关内容

最新更新