首页 > 解决方案 > 我在打字稿中有一个自定义事件,我只想创建一次自定义事件对象,而后者想使用相同的对象

问题描述

假设

export class  PublishEvent {
    public static DispatchEvent(eventName:string,data:any){
        const customEvent = new CustomEvent(eventName, {
            "detail": data
        });
        document.dispatchEvent(customEvent);
    }
}

在我的代码中,当我调用 DispatchEvent 时,我每次都需要创建新的 CustomEvent。有没有所以我会创建一次对象并进一步更新数据。

标签: javascripttypescript

解决方案


推荐阅读