有没有办法像在 jQuery 中那样将负载添加到原始 JavaScript 事件?
// jQuery
$(document).trigger('event', payload);
// Vanilla JS
window.dispatchEvent(new Event('event', ???
我想我必须使用我自己的某种事件总线?
最佳答案
尝试使用 CustomEvent
,参见 Creating and triggering events
function eventHandler(e) {
console.log("detail:", e.detail);
}
window.addEventListener("build", eventHandler);
window.dispatchEvent(new CustomEvent("build", {"detail": {"abc":123}}));
关于javascript - 将负载添加到原始 JavaScript 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30554145/