Angular 2 RC 2如何将路由器注入自定义ExceptionHandler

Har*_*aka 5 angular2-routing angular

我正在使用Angular 2 RC2.我需要将Angular 2路由器注入我的自定义ExceptionHandler类.但是我收到以下错误

错误:错误:无法解析'ErrorHandler'(?)的所有参数.确保所有参数都使用Inject进行修饰或具有有效的类型注释,并且"ErrorHandler"使用Injectable进行修饰.

我确实试过装饰私有路由器:使用@Inject的路由器无济于事.我正在使用打字稿,因此我认为我不需要@Inject属性.

我的自定义ExceptionHandler看起来像这样

import { ExceptionHandler } from '@angular/core';
import { Router } from '@angular/router';

export class ErrorHandler extends ExceptionHandler{

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

    }

    call(error, stackTrace = null, reason = null) {
        console.log(error);
        this.router.navigate(['/error']);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的主要看起来像这样

import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';

import { provide, ExceptionHandler } from '@angular/core';
import { ErrorHandler } from './error-handler/error-handler';

import { HTTP_PROVIDERS } from '@angular/http';
import { ROUTER_PROVIDERS } from '@angular/router';

bootstrap(AppComponent, [
    HTTP_PROVIDERS,
    ROUTER_PROVIDERS,
    provide(ExceptionHandler, {useClass: ErrorHandler})
]);
Run Code Online (Sandbox Code Playgroud)

为什么我收到此错误?在ExceptionHandler实例化时,路由器是否可注入?

完整的源代码可在此处获得

https://github.com/harindaka/angular2-seed-typescript/tree/b368315ce6608085f3154a03bc53f0404ce16495

Gün*_*uer 3

更新 ExceptionHandler已重命名为ErrorHandler /sf/answers/2466731991/

原来的

我猜这是循环依赖造成的。您可以通过注入注入器并强制获取路由器来解决此问题:

export class ErrorHandler extends ExceptionHandler{

    private router: Router 
    constructor(inject:Injector){
        this.router = injector.get(Router);
        super(null, null);

    }

    call(error, stackTrace = null, reason = null) {
        console.log(error);
        this.router.navigate(['/error']);
    }
}
Run Code Online (Sandbox Code Playgroud)