简体   繁体   English

如何在自定义 ErrorHandler 中解决 Angular 8 应用程序中的“无法解析 […] 的所有参数”问题?

[英]How to solve “Can't resolve all parameters for […]” issue in Angular 8 application in a custom ErrorHandler?

In my Angular 8 app I want to handle errors with my custom error handler.在我的 Angular 8 应用程序中,我想使用自定义错误处理程序处理错误。 I have this code:我有这个代码:

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpClientModule,
  ],
  providers: [
    AppErrorHandler,
    { provide: ErrorHandler, useClass: AppErrorHandler },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

error-handler.ts

import { ErrorHandler, Injector } from '@angular/core';
import { Router } from '@angular/router';
import { throwError } from 'rxjs';

export class AppErrorHandler implements ErrorHandler {
  constructor(
    private injector: Injector,
  ) {}

  handleError(error: any) {
    const router = this.injector.get(Router);
    // ...some case I navigate user to a custom error page, like "not found"
  }
}

In the compiler everything is green, but in the console at loading application I get this error:在编译器中,一切都是绿色的,但是在加载应用程序的控制台中,我收到了这个错误:

Uncaught Error: Can't resolve all parameters for AppErrorHandler: (?).
    at syntaxError (compiler.js:2430)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (compiler.js:18984)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (compiler.js:18877)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getInjectableTypeMetadata (compiler.js:19099)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getProviderMetadata (compiler.js:19108)
    at compiler.js:19046
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getProvidersMetadata (compiler.js:19006)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (compiler.js:18725)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules (compiler.js:26029)

Why I get this error message and how can I solve this?为什么我会收到此错误消息,我该如何解决?

Try this in your error-handler.ts在你的error-handler.ts中试试这个

@Injectable() 
export class AppErrorHandle extends ErrorHandler {
.........
.......

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

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