1

ich eine Klasse gemacht zu verlängern versuchen, die die Exception in Angular 2. ich das Bootstrap erweitert:Kein Anbieter Ausnahme, wenn die Winkel 2 Exception Klasse

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

Das funktioniert gut, ergeben sich die Probleme, wenn ich versuche zu injizieren mein API-Dienst. Ich versuchte dies:

@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); 
    } 
} 

Dann meine ArrayLogger Klasse:

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); 

    } 

    (...) 

} 

Das gibt mir den Fehler:

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] 

Wie kann ich meine exceptionAPIService injizieren richtig?

Antwort

1

Da Sie ExceptionApiService in CustomExceptionHandler sind Injektion, müssen Sie es schaffen, auch:

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