gpt4 book ai didi

angular - 在Angular 8+中实现ErrorHandler类会在运行时给出异常

转载 作者:行者123 更新时间:2023-12-03 08:18:54 24 4
gpt4 key购买 nike

this snippet is from app.module.ts. I just created an angular 9 application 
and tried to implement ErrorHandler class to catch error but it is showing
the error at run time and page does not load.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ErrorHandler } from '@angular/core';

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
{provide: ErrorHandler, useClass: MyErrorHandler}
],
bootstrap: [AppComponent]
})

class MyErrorHandler implements ErrorHandler {
handleError(error) {
alert(error);
// do something with the exception
}
}
export class AppModule { }


Exception in browser:


ASSERTION ERROR: Type passed in is not NgModuleType, it does not have 'ɵmod' property.
at throwError (core.js:1344)
at assertNgModuleType (core.js:2951)
at compileNgModuleFactory__POST_R3__ (core.js:42275)
at PlatformRef.bootstrapModule (core.js:42663)
at Module../src/main.ts (main.ts:11)
at __webpack_require__ (bootstrap:79)
at Object.0 (main.ts:12)
at __webpack_require__ (bootstrap:79)
at checkDeferredModules (bootstrap:45)
at Array.webpackJsonpCallback [as push] (bootstrap:32)

最佳答案

将您的MyErrorHandler类移动到一个单独的文件中,并将其导入到AppModule中,它应该可以工作。
Angular使用 typescript 装饰器,这些装饰器在它们所属的每个类之上定义。在您的情况下,您要将装饰器@NgModule添加到类MyErrorHandler中,这样Angular找不到主AppModule的正确配置,这就是为什么在编译时会收到此类错误。

关于angular - 在Angular 8+中实现ErrorHandler类会在运行时给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62031543/

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