我在项目中实现了以下ErrorHandler
:
export class RavenErrorHandler implements ErrorHandler {
handleError(err: any): void {
Raven.captureException(err.originalError);
}
}

我也在我的AppModule
中正确提供了它。但我也想使用AlertService和路由器在我远程记录错误后将用户路由到一般错误页面。
如何将这些服务注入自定义ErrorHandler?
如果我只是将它们注入构造函数,应用程序永远不会加载(卡在Loading...
)并且控制台上根本没有显示任何内容!
更新:我将@Injectable添加到上面的RavenErrorHandler
,现在我收到以下错误:
[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
&#13;
答案 0 :(得分:3)
您不能直接注入Router
,但可以在Router
方法中使用注入器并获取handlerError
实例。
不要忘记将@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);