在 Angular 自定义错误处理程序中使用服务

Using Service in Angular Custom ErrorHandler

我在我的项目中实现了以下ErrorHandler

export class RavenErrorHandler implements ErrorHandler {
  handleError(err: any): void {
    Raven.captureException(err.originalError);
  }
}

我也在 AppModule 中正确提供了它。但是我也想在我像上面那样远程记录错误后使用 AlertService 和 Router 将用户路由到一般错误页面。

如何将这些服务注入自定义 ErrorHandler?

如果我只是将它们注入构造函数,应用程序永远不会加载(停留在 Loading...)并且控制台上根本不会显示任何内容!

更新:我在上面的 RavenErrorHandler 中添加了 @Injectable,现在出现以下错误:

[Error] Unhandled Promise rejection: (8) "Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef (\"[ERROR ->]\"): in NgModule AppModule in ./AppModule@-1:-1" "; Zone:" "
<root>" "; Task:" "Promise.then" "; Value:" Error: Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1 — compiler.es5.js:10921 parse — compiler.es5.js:10921 compile — compiler.es5.js:17394
  _compileModule — compiler.es5.js:25652 run — zone.js:141 (anonymous function) — zone.js:805 runTask — zone.js:181 drainMicroTaskQueue — zone.js:574 promiseReactionJob (anonymous function) (vendor.bundle.js:98814) onUnhandledError (polyfills.bundle.js:2613)
  handleUnhandledRejection (polyfills.bundle.js:2637) _loop_1 (polyfills.bundle.js:2628) microtaskDrainDone (polyfills.bundle.js:2632) drainMicroTaskQueue (polyfills.bundle.js:2565) promiseReactionJob [Error] Error: Uncaught (in promise): Error: Provider
  parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1 parse@http://localhost:4200/vendor.bundle.js:64024:28 compile@http://localhost:4200/vendor.bundle.js:70497:29 _compileModule@http://localhost:4200/vendor.bundle.js:78755:80
  run@http://localhost:4200/polyfills.bundle.js:2124:49 http://localhost:4200/polyfills.bundle.js:2788:60 runTask@http://localhost:4200/polyfills.bundle.js:2164:57 drainMicroTaskQueue@http://localhost:4200/polyfills.bundle.js:2557:42 promiseReactionJob@[native
  code] — zone.js:757 (anonymous function) (vendor.bundle.js:98814) onUnhandledError (polyfills.bundle.js:2615) handleUnhandledRejection (polyfills.bundle.js:2637) _loop_1 (polyfills.bundle.js:2628) microtaskDrainDone (polyfills.bundle.js:2632) drainMicroTaskQueue
  (polyfills.bundle.js:2565) promiseReactionJob

您不能直接注入 Router 但您可以使用注入器并在 handlerError 方法中获取 Router 实例。

不要忘记将 @Injectable() 装饰器添加到您的自定义处理程序

@Injectable()
export class MyErrorHandler extends ErrorHandler {
  constructor(private service: TestService, private zone: NgZone, private inj: Injector) {
    super();
  }

  handleError(err) {
    let router = this.inj.get(Router);
    debugger;
  }
}

测试一下here

不要看下面:)这是内在的逻辑

因此,从我们的代码生成的模块工厂将如下所示:

AppModuleInjector.prototype.createInternal = function() {
  var self = this;
  self._CommonModule_0 = new jit_CommonModule27();
  self._TestService_1 = new jit_TestService28();
  self._ErrorHandler_2 = new jit_MyErrorHandler29(self._TestService_1,self.parent.get(jit_NgZone16),self);