-
创建自定义事件传递:const customEvent = new CoustomEvent(coustomEventName, config);
- { detail } = config: detail作为一个内置的只读属性key,是一个对象, 对象内可以传递任何数据类型
{ detail: { isnumber: true, customData: { key: 90 }, ... } }
- { detail } = config: detail作为一个内置的只读属性key,是一个对象, 对象内可以传递任何数据类型
-
派发执行事件:
window.dispatchEvent(customEvent); -
监听自定义事件的触发:
window.addEventListener(coustomEventName, (config) => {...});
示例:
// project 1: 注册事件,发出信息
const customEvent = new CustomEvent('Example_Custom_Event_Name', {
detail: {
isTest: true
}
});
window.dispatchEvent(event);
// project 2: 监听事件,接收信息
window.addEventListener('Example_Custom_Event_Name', (config) => {
console.log('Example_Custom_Event_Name config >>>', config, config.detail.isTest);
// ... do something
});