1

を拡張しようとしている私はこれをブートストラップ角度2にexceptionHandlerのを拡張するクラスを作っ:ないプロバイダ例外角度2 exceptionHandlerのクラスに

bootstrap(App, [ 
    APP_ROUTER_PROVIDERS, 
    HTTP_PROVIDERS, 
    provide(ExceptionHandler, { useClass: CustomExceptionHandler }), 

これがうまく機能し、私は注入しようとすると問題が発生私のAPIサービス。私ArrayLoggerクラス次に

@Injectable() 
export class CustomExceptionHandler extends ExceptionHandler { 

    constructor(_exceptionApiService: ExceptionApiService) { 
    super(new ArrayLogger(_exceptionApiService), true); 
    } 

    call(error, stackTrace = null, reason = null) { 
    super.call(error, stackTrace, reason); 
    } 
} 

:私はこれを試してみました

zone.min.js:1 Unhandled Promise rejection: (SystemJS) EXCEPTION: Error during instantiation of ApplicationRef_! (ApplicationRef -> ApplicationRef_). 
    ORIGINAL EXCEPTION: No provider for ExceptionApiService! (ExceptionHandler -> ExceptionApiService) 
    ORIGINAL STACKTRACE: 
    Error: DI Exception 
     at NoProviderError.BaseException [as constructor] (http://localhost:4200/vendor/@angular/core/src/facade/exceptions.js:27:23) 
     at NoProviderError.AbstractProviderError [as constructor] 

私はきちんと私のexceptionAPIServiceを注入することができます方法:

export class ArrayLogger { 

    private _exceptionApiService: ExceptionApiService; 

    constructor(_exceptionApiService: ExceptionApiService) { 
    this._exceptionApiService = _exceptionApiService; 
    } 

    res = []; 
    log(s: any): void { this.res.push(s); } 
    logError(s: any): void { this.res.push(s); } 
    logGroup(s: any): void { this.res.push(s); } 
    logGroupEnd() { 

    let error: Error; 

    let message = this.res[0].replace('EXCEPTION: ', ''); 
    let stack = []; 
    let index = this.findStackTrace(this.res); 

    if (index === -1) { 
     stack.push('Could not find stackTrace!'); 
    } else { 
     this.res[index].split('\n').map(line => stack.push(line.trim())); 
    } 

    error = new Error({ 
     url: window.location.href, 
     error: message, 
     stack: stack, 
     browser: this.getBrowser(navigator.userAgent), 
     userAgent: navigator.userAgent, 
     platform: navigator.platform, 
     datetime: '' 
    }); 

    this._exceptionApiService.post(error); 

    } 

    (...) 

} 

をこれは私にエラーを与えて?

答えて

1

あなたはCustomExceptionHandlerExceptionApiServiceを注入しているので、あなたも、それを提供する必要があります。

bootstrap(App, [ 
    APP_ROUTER_PROVIDERS, 
    HTTP_PROVIDERS, 
    ExceptionApiService, 
    provide(ExceptionHandler, { useClass: CustomExceptionHandler }), 
関連する問題