gpt4 book ai didi

javascript - 从 CustomEvent 获取事件的名称

转载 作者:行者123 更新时间:2023-11-30 11:10:06 24 4
gpt4 key购买 nike

我在一个函数上有一个 CustomEvent 参数,我想检索事件的名称,即它是用 (new CustomEvent('foo')) 构造的字符串

为了引用 https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent

    public addEvent(iChannel: number, oEvent: CustomEvent)
{
if (App.isDebugging)
{
console.log(CustomEvent+' emitted '+CustomEvent.typeArg+' on channel '+iChannel);
console.log(CustomEvent);
}
this.aEventChannels[iChannel].dispatchEvent(oEvent);
}

//what lib.dom.d.ts says about this
declare var CustomEvent: {
prototype: CustomEvent;
new<T>(typeArg: string, eventInitDict?: CustomEventInit<T>):
CustomEvent<T>;
};

我试过 typetypeArg。两者都返回错误:

tsc --build src-webui/ts
src-webui/ts/App.ts(162,53): error TS2339: Property 'typeArg' does not exist on type '{ new <T>(typeArg: string, eventInitDict?: CustomEventInit<T> | undefined): CustomEvent<T>; prototype: CustomEvent<any>; }'.`

我又回到了 TS 的摇摆中,所以我确信这很简单,正确的语法对我来说并不是“点击”。

感谢任何指点,谢谢!

PS:这是针对浏览器的。

最佳答案

看起来您正在尝试获取 CustomEvent 类的 .type 属性,而不是传递的 oEvent 对象到功能。尝试改用它:

public addEvent(iChannel: number, oEvent: CustomEvent) {
if (App.isDebugging) {
console.log('Emitted ' + oEvent.type + ' on channel ' + iChannel);
console.log(oEvent);
}
this.aEventChannels[iChannel].dispatchEvent(oEvent);
}

关于javascript - 从 CustomEvent 获取事件的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54051195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com