简体   繁体   English

全局ExceptionHandler中的Angular 2重定向

[英]Angular 2 redirect in global ExceptionHandler

I'm trying to implement an ExceptionHandler that redirects the user to an error page. 我正在尝试实现一个将用户重定向到错误页面的ExceptionHandler。

For that I need to inject a router object like so: 为此,我需要注入一个路由器对象,如下所示:

@Injectable()
class RedirectExceptionHandler extends ExceptionHandler {

  constructor(private router: Router) {
    super(null, null);
  }

  call(error: any, stackTrace: any = null, reason: any = null) {
    // ...log and redirect to error page
  }
}

However, when I run this code I get an exception: 但是,当我运行此代码时,我得到一个异常:

(index):87 Error: EXCEPTION: Error during instantiation of ApplicationRef_! (ApplicationRef -> ApplicationRef_).
    ORIGINAL EXCEPTION: Cannot instantiate cyclic dependency! (ExceptionHandler -> Router -> ApplicationRef)
    ORIGINAL STACKTRACE:
    Error: DI Exception
        at CyclicDependencyError.BaseException [as constructor] (http://localhost:1080/node_modules/@angular/core/src/facade/exceptions.js:17:23)
        at CyclicDependencyError.AbstractProviderError[as constructor] (http://localhost:1080/node_modules/@angular/core/src/di/reflective_exceptions.js:39:16)
        at new CyclicDependencyError(http://localhost:1080/node_modules/@angular/core/src/di/reflective_exceptions.js:102:16)
        at ReflectiveInjector_._new (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:613:19)
        at ReflectiveInjectorDynamicStrategy.getObjByKeyId (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:270:50)
        at ReflectiveInjector_._getByKeyDefault (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:795:38)
        at ReflectiveInjector_._getByKey (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:767:25)
        at ReflectiveInjector_._getByReflectiveDependency (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:757:21)
        at ReflectiveInjector_._instantiate (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:654:36)
        at ReflectiveInjector_._instantiateProvider (http://localhost:1080/node_modules/@angular/core/src/di/reflective_injector.js:626:25)
    Evaluating http://localhost:1080/app/main.js
    Error loading http://localhost:1080/app/main.js

Is there a solution for this? 这有解决方案吗? Is it possible to instantiate the router in the call method instead, thereby avoiding the cyclic dependency during object creation? 是否可以在调用方法中实例化路由器,从而避免在对象创建期间出现循环依赖?

Inject Angular's injector and get the router instance at a later stage, after your error handler component has already been instantiated: 在已经实例化错误处理程序组件之后,注入Angular的注入器并在稍后阶段获取路由器实例:

constructor(private injector:Injector) {
    super(null);
}

ngOnInit() {
    this.router = this.injector.get(Router);
}

So later on you can use the router instance: 所以稍后您可以使用路由器实例:

call(exception:any, stackTrace?:any, reason?:string):void {
    this.router.navigate(...);
}

暂无
暂无

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

相关问题 Angular JS exceptionHandler中的循环依赖 - Circular Dependency in Angular JS exceptionHandler 角JS:$ exceptionHandler没有尝试或抛出块? - Angular JS : $exceptionHandler without try or throw block? 从 chrome repl 触发 angular $exceptionHandler - Fire off angular $exceptionHandler from chrome repl Angular2 ExceptionHandler-检查错误对象是否为Response - Angular2 ExceptionHandler - check if error object is Response 使用VS 2013在Typescript中装饰AngularExceptionHandler服务 - Decorating angular exceptionHandler service in Typescript using VS 2013 鸡肉和鸡蛋问题与angular,$ exceptionHandler和$ http中的依赖项注入 - Chicken and egg issue with dependency injection in angular, $exceptionHandler and $http 将$ http注入角度工厂($ exceptionHandler)会导致循环依赖 - Injecting $http into angular factory($exceptionHandler) results in a Circular dependency Angular $ http服务是否对Http错误响应调用$ exceptionHandler - Does Angular $http Service Call into the $exceptionHandler on Http Error Responses 尝试覆盖$ exceptionHandler时,简单的javascript异常在Angular Controller初始化上发生7次 - Simple javascript exception happens 7 times on Angular Controller init when trying to override $exceptionHandler Angular:在正常情况下,我可以确保在$ exceptionHandler之前配置我的日志记录模块吗? - Angular: Can I ensure that my logging module gets configured before $exceptionHandler under normal circumstances?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM