2016-07-18 2 views
1

저는 약간의 주위를 검색했는데 혹시 혹서를 극복하는데 도움이되는 것을 발견하지 못했습니다. 필자는 필요에 따라 Angular2 ExceptionHandler를 구현했다고 생각하지만 결코 호출되지 않습니다.Angular2 ExceptionHandler가 작동하지 않습니다.

Main.ts :

import { bootstrap } from '@angular/platform-browser-dynamic'; 
import { disableDeprecatedForms, provideForms } from '@angular/forms'; 
import { AppComponent } from './app.component'; 
import { ExceptionHandler, Injector , provide, Provider} from '@angular/core'; 
import { APP_ROUTER_PROVIDERS } from './app.routes'; // <-- load in global routes and bootstrap them 
import { ErrorHandler } from './errorhandler/error-handler.component'; 

bootstrap(AppComponent,[ 
    APP_ROUTER_PROVIDERS, 
    disableDeprecatedForms(), 
    provideForms(), 
    provide(ExceptionHandler, {useClass: ErrorHandler}) 
]) 

오류 handler.component.ts : 내가 시험 (내 서비스 중 하나에서 발생하는 강제하고있어 HTTP 오류가

import { Component, Injectable, ExceptionHandler } from '@angular/core'; 
import { ROUTER_DIRECTIVES } from '@angular/router'; 
import { APP_PROVIDERS } from '../app.providers'; 

@Component({ 
    moduleId: module.id, 
    template: `Error happened here!!!!`, 
    directives: [ ROUTER_DIRECTIVES ], 
    providers: [ APP_PROVIDERS ] 
}) 

@Injectable() 
export class ErrorHandler extends ExceptionHandler { 

    call (exception: any, stackTrace?: any, reason?: string): void { 


    } 

} 

예외 처리) 내 "전역"처리기가 ​​호출 될 것이라고 예상 할 것이다. 내가 놓친 게 있니? 안타깝게도 지금 당장은 많은 문서가 없습니다 ...

TIA.

제임스

답변

3

그게 문제지만 오류 처리기로 Component를 사용하지 않는 경우 없음 아이디어. 그것은 그것을하는 방법이 아닙니다.

ExceptionHandler 인터페이스를 구현하고 연장하지 않아도됩니다 (필요하지는 않지만 더 차갑게 보입니다). 이

입니다

export class ErrorHandler implements ExceptionHandler { 
    call(error, stackTrace = null, reason = null) { 
    // do something with the exception 
    } 
} 

당신은 당신의 부트 스트랩 방식을 유지할 수 있습니다 @Injectable()에 대한 필요 (아직 ... 어쩌면이 핸들러는 미래에 종속성이 없습니다, 다음이 필요합니다)

관련 문제