gpt4 book ai didi

javascript - 尝试扩展 Angular 2ExceptionHandler 类时没有提供程序异常

转载 作者:行者123 更新时间:2023-11-27 22:43:01 24 4
gpt4 key购买 nike

我创建了一个扩展 Angular 2 中的 ExceptionHandler 的类。我引导了这个:

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

这工作得很好,当我尝试注入(inject)我的 API 服务时就会出现问题。我试过这个:

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

然后是我的 ArrayLogger 类:

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

}

(...)

}

这给了我错误:

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]

如何正确注入(inject)我的ExceptionAPIService?

最佳答案

由于您要在 CustomExceptionHandler 中注入(inject) ExceptionApiService,因此您也需要提供它:

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

关于javascript - 尝试扩展 Angular 2ExceptionHandler 类时没有提供程序异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com