简体   繁体   English

ReferenceError: HTMLElement 未在 TypeScript 中定义

[英]ReferenceError: HTMLElement is not defined in TypeScript

I'm new to TypeScript, and frankly, I may be doing this wrong.我是 TypeScript 的新手,坦率地说,我可能做错了。 But...I'm trying to create a "KeyUp Listener" for text-boxes that will "notify" when the user has stopped typing.但是......我正在尝试为文本框创建一个“KeyUp 侦听器”,当用户停止输入时将“通知”。 However, when I try to register the class, I get the following error:但是,当我尝试注册 class 时,出现以下错误:

Exception: Call to Node module failed with error: Prerendering failed because of error: ReferenceError: HTMLElement is not defined异常:调用节点模块失败并出现错误:预渲染因错误而失败:ReferenceError:HTMLElement 未定义

REGISTRATION:登记:

// MODULES
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

// COMPONENTS
import { AppComponent } from './components/app/app.component'
import { HeaderComponent } from './components/shared/components/shared-header.component';
import { SampleIndexComponent } from './components/samples/sample.index.component';
import { SampleFormComponent } from './components/samples/sample.form.component';
import { StatusFilterComponent } from './components/samples/filters/status-filter.component';
import { AuthorFilterComponent } from './components/samples/filters/author-filter.component';

// LISTENERS
import { TextboxKeyUpListener } from "./components/shared/services/textbox.keyup.listener";

@NgModule({
    bootstrap: [ AppComponent ],
    declarations: [
        AppComponent,
        HeaderComponent,
        SampleIndexComponent,
        StatusFilterComponent,
        AuthorFilterComponent,
        SampleFormComponent
    ],
    imports: [
        UniversalModule, // <-- Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
        FormsModule,
        ReactiveFormsModule,
        RouterModule.forRoot([
            { path: '', redirectTo: 'samples', pathMatch: 'full' },
            { path: 'samples', component: SampleIndexComponent },
            { path: 'form', component: SampleFormComponent },
            { path: '**', redirectTo: 'samples' }
        ])
    ],
    providers: [AuthorFilterNotifier, StatusFilterNotifier, TextboxKeyUpListener]
})
export class AppModule
{
    constructor() { }   
}

NOTIFIER LOOKS LIKE:通知器看起来像:

// MODULES
import { Injectable, Inject } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class TextboxKeyUpListener {

    // CONSTRUCTORS
    constructor(private textbox: HTMLInputElement)
    {
        this.init();
    }

    // PROPERTIES - private
    private typingTimer: NodeJS.Timer;
    private typingTimerInterval: number = 1000;
    private notifyDoneTyping = new Subject<string>();

    // PROPERTIES - public
    public notifyDoneTyping$ = this.notifyDoneTyping.asObservable();

    // METHODS - public
    private keyUp(ev: KeyboardEvent)
    {
        global.clearTimeout(this.typingTimer);
        if(this.textbox.value)
            this.typingTimer = global.setTimeout(this.notify, this.typingTimerInterval);
    }

    // METHODS - private
    private init()
    {
        this.textbox.onkeyup = this.keyUp;
    }

    private notify()
    {
        this.notifyDoneTyping.next(this.textbox.value);
        console.log("Done Typing")
    }
}

Some time ago I had a problem in having Typescript recognize some basic DOM classes, which has been solved adding 前一段时间,我在让Typescript识别一些基本的DOM类时遇到了问题,该问题已通过添加解决

"lib": [
  "es2016",
  "dom"
]

to tsconfig.json file 到tsconfig.json文件

I am not sure this can help your case, but it may be worth trying 我不确定这是否可以帮助您解决问题,但可能值得尝试

========================================================================== ================================================== ========================

ADDITIONAL THOUGHTS TRIGGERED BY THE COMMENTS CHAIN - NOT RELATED TO THE ORIGINAL QUESTION 注释链触发的其他思想-与原始问题无关

Use of debounce Rx operator to signal end-of-typing 使用去抖动的Rx运算符来指示打字结束

If you need to signal end of typing in a simple and elegant way, you may want to consider to use Rx and the debounce operator. 如果您需要用一种简单而优雅的方式来告知输入结束,则可以考虑使用Rxdebounce操作符。 The idea is to create an Observable out of the event stream coming from the Front End element (in your case the Input element I guess) and use debounce to have an event emitted if no new value has been received from the input stream within a specified amount of time. 这个想法是从前端元素(在我看来是输入元素)的事件流中创建一个Observable,并在指定的范围内未从输入流中收到新值时,使用debounce来发出事件。多少时间。 You can look at these links for more details ( What does RxJS.Observable debounce do? , https://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html ) - many more examples are available online 您可以查看这些链接以了解更多详细信息( RxJS.Observable防反跳功能是什么?https: //blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html )-在线有更多示例

Communication among components 组件之间的通讯

I understand you have 2 components that need to communicate. 我了解您有2个组件需要沟通。 Specifically the "listener" needs to tell the "other component" that typing is finished. 具体来说, “侦听器”需要告诉“其他组件”打字已经完成。

If the "other component" always contains the "listener" you may use @Output to define output events that the "listener" can emit and the "other component" can listen to. 如果“其他组件”始终包含“侦听器” ,则可以使用@Output定义“侦听器”可以发出且“其他组件”可以侦听的输出事件。

If the "listener" is not contained by the "other component" , then the use of a service to communicate can be considered even if you have to also consider that services are Singletons (at least within their scope). 如果“侦听器”未包含在“其他组件”中 ,那么即使您还必须考虑服务属于单例(至少在其范围之内),也可以考虑使用服务进行通信。 Being a Singleton means that if you have more than one Input field you want to listen to for end-of-typing, than you need to figure out how to map the Singleton service with the many Inputs. 成为Singleton意味着,如果您要监听多个输入字段以结束输入,则需要弄清楚如何将Singleton服务与许多Inputs映射。

In case the UI is complex, you can consider to use a central store as per redux pattern (redux store pattern is available in Angular through https://github.com/ngrx/store or https://github.com/angular-redux/store - this is an interesting link that explains pros and cons of a redux-store based architecture) 如果用户界面很复杂,您可以考虑按照redux模式使用中央存储(redux存储模式可通过https://github.com/ngrx/storehttps://github.com/angular-在Angular中使用redux / store-这是一个有趣的链接 ,解释了基于redux-store的架构的优缺点)

Maybe this is actually an eslint and not a typescript error.也许这实际上是一个eslint而不是typescript错误。

For me it solved completely by adding browser to the list of environments on .eslintrc.js .对我来说,它通过将browser添加到.eslintrc.js的环境列表中完全解决了。

{
  ...
  env:    {
    ...
    browser: true,
    ...
  },
  ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM