尝试扩展 angular 2 exceptionHandler class 时没有提供程序异常

No provider exception when trying to extend the angular 2 exceptionHandler class

我做了一个 class 扩展了 Angular 中的 ExceptionHandler 2。我 bootstrap 这个:

bootstrap(App, [
  APP_ROUTER_PROVIDERS,
  HTTP_PROVIDERS,
  provide(ExceptionHandler, { useClass: CustomExceptionHandler }),

这很好,当我尝试注入我的 API 服务时出现问题。我试过这个:

@Injectable()
export class CustomExceptionHandler extends ExceptionHandler {

  constructor( _exceptionApiService: ExceptionApiService) {
    super(new ArrayLogger(_exceptionApiService), true);
  }

  call(error, stackTrace = null, reason = null) {
    super.call(error, stackTrace, reason);
  }
}

然后我的 ArrayLogger class:

export class ArrayLogger {

  private _exceptionApiService: ExceptionApiService;

  constructor(_exceptionApiService: ExceptionApiService) {
    this._exceptionApiService = _exceptionApiService;
  }

  res = [];
  log(s: any): void { this.res.push(s); }
  logError(s: any): void { this.res.push(s); }
  logGroup(s: any): void { this.res.push(s); }
  logGroupEnd() {

    let error: Error;

    let message = this.res[0].replace('EXCEPTION: ', '');
    let stack = [];
    let index = this.findStackTrace(this.res);

    if (index === -1) {
      stack.push('Could not find stackTrace!');
    } else {
      this.res[index].split('\n').map(line => stack.push(line.trim()));
    }

    error = new Error({
      url: window.location.href,
      error: message,
      stack: stack,
      browser: this.getBrowser(navigator.userAgent),
      userAgent: navigator.userAgent,
      platform: navigator.platform,
      datetime: ''
    });

    this._exceptionApiService.post(error);

  }

  (...)

}

这给了我错误:

zone.min.js:1 Unhandled Promise rejection: (SystemJS) EXCEPTION: Error during instantiation of ApplicationRef_! (ApplicationRef -> ApplicationRef_).
    ORIGINAL EXCEPTION: No provider for ExceptionApiService! (ExceptionHandler -> ExceptionApiService)
    ORIGINAL STACKTRACE:
    Error: DI Exception
        at NoProviderError.BaseException [as constructor] (http://localhost:4200/vendor/@angular/core/src/facade/exceptions.js:27:23)
        at NoProviderError.AbstractProviderError [as constructor] 

如何正确注入我的异常API服务?

由于您要在 CustomExceptionHandler 中注入 ExceptionApiService,因此您也需要提供它:

bootstrap(App, [
  APP_ROUTER_PROVIDERS,
  HTTP_PROVIDERS,
  ExceptionApiService,
  provide(ExceptionHandler, { useClass: CustomExceptionHandler }),