本文介绍了加载CustomExceptionHandler时,Angular 2注入器找不到服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当尝试将服务注入到我的CustomExceptionHandler中时,Angular注入器找不到该服务.
When trying to inject a service into my CustomExceptionHandler, the Angular injector cannot find the service.
错误:未捕获无法解析CustomExceptionHandler的所有参数:(?).
设置:
CustomExceptionHandler
import { ExceptionHandler } from '@angular/core';
import { ServiceA } from '../services/a.service';
export class CustomExceptionHandler extends ExceptionHandler {
constructor(private serviceA: ServiceA) {
super(null, null);
}
call(error, stackTrace = null, reason = null) {
//do something with the service
}
}
应用模块
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
ServiceA,
{ provide: ExceptionHandler, useClass: CustomExceptionHandler },
],
bootstrap: [AppComponent],
})
export class AppModule { }
推荐答案
更新 ExceptionHandler
重命名为 ErrorHandler
原始
将 @Injectable()
添加到您的CustomExceptionHandler类中.
Add @Injectable()
to your CustomExceptionHandler class.
在app.module.ts中添加CustomExceptionHandler作为您的另一个提供者:
Add CustomExceptionHandler as another one of your providers in the app.module.ts:
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
ServiceA,
CustomExceptionHandler,
{ provide: ExceptionHandler, useClass: CustomExceptionHandler },
],
bootstrap: [AppComponent],
})
export class AppModule { }
这篇关于加载CustomExceptionHandler时,Angular 2注入器找不到服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!