简体   繁体   English

在事件处理程序中使用另一个类中的方法并绑定当前类上下文时,打字稿错误TS2339

[英]typescript error TS2339 when using method from another class in event handler and binding current class context

I have a service which handles all of my dom manipulation called DomService 我有一个处理我所有dom操作的服务,称为DomService

I have another service handling modal functionality called ModalService 我还有另一个服务处理模式功能,称为ModalService

In ModalService I'm binding a few events and giving it a method from DomService as the listener which looks like: ModalService我绑定了一些事件,并从DomService提供了一个方法作为侦听器,如下所示:

document.body.addEventListener('focus', this.domService.keyPress.bind(this), true);

and then keyPress looks something like: 然后keyPress看起来像:

keyPress(event){
    if (event.which === key.escape) {
        event.preventDefault();
        this.hide();
    }
}

It works fine, the problem is typescript stills sees this as a reference to the DomService class when it's actually bound to the ModalService class, so it's telling me the hide property doesn't exist on type DomService 它工作正常,问题是打字稿剧照认为this是对一个参考DomService时,它实际上是绑定到类ModalService类,所以它告诉我的hide属性的类型不存在DomService

Is there anything I can do to get typescript to chill? 有什么我可以做的让打字稿变冷的方法?

You can change the context of this in any function by adding it (and its type) as the first argument to the function. 你可以改变的背景下this把它加(及其类型)作为第一个参数传递给函数的任何功能。 So to change the context of this inside keyPress to be a ModalService , you would do: 因此,要改变的背景下this里面keyPress是一个ModalService ,你会怎么做:

keyPress(this: ModalService, event) {
    if (event.which === key.escape) {
        event.preventDefault();
        this.hide(); // Now is OK.
    }
}

You can read more about that feature here . 您可以在此处阅读有关该功能的更多信息。

In addition to that, you can always add a line // @ts-ignore above any line creating an error to silence it, if the above solution isn't sufficient. 除此之外,如果上述解决方案还不够,您总是可以在任何行上方添加// @ts-ignore ,从而创建一个错误以使其静音。

You can use EventEmitter and handle (subscribe) "emit" event. 您可以使用EventEmitter并处理(订阅)“发出”事件。

ModalService 模态服务

this.subscriptionHide = this.domService.getHideEmitter()
  .subscribe(() => this.hide());

DomService DomService

hideEmitter: EventEmitter<any> = new EventEmitter();

keyPress(event){
    if (event.which === key.escape) {
        event.preventDefault();
        this.hideEmitter.emit(null);
    }
}

getHideEmitter() {
    return this.hideEmitter;
}

Try change your functions to arrow functions. 尝试将功能更改为箭头功能。

Change 更改

keyPress(event) {

To

keyPress = (event) => {

You can read about arrow functions in typescript here 您可以在此处阅读打字稿中的箭头功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:JavaScript到Typescript错误 - error TS2339: JavaScript to Typescript error Typescript TS2339 属性在类型上不存在 - 类声明 - Typescript TS2339 Property doesn't exist on type - Class decleration Typescript 无法编译类型不存在的错误 TS2339 - Typescript can't compile type not existing Error TS2339 为什么在键入Redux-saga调用参数时Typescript会引发错误-TS2339 - Why does Typescript throw an error when typing Redux-saga call parameter - TS2339 从 js 迁移到 ts (ts2345) (ts2339) 时出现赛普拉斯错误 - Cypress error while migrating from js to ts (ts2345) (ts2339) 打字稿错误TS2339:'Window'类型中不存在属性'webkitURL' - Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window' TypeScript:错误TS2339:类型“ Window”上不存在属性“ CustomEvent” - TypeScript: error TS2339: Property 'CustomEvent' does not exist on type 'Window' ts2339怎么解决? 使用带有测试库/反应的打字稿 - How to solve ts2339? Using typescript with testing-library/react 如何解决打字稿错误 TS2339:“IntrinsicAttributes &amp; …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM