Angular 角度2喷油器can';加载CustomExceptionHandler时找不到服务
尝试将服务注入CustomExceptionHandler时,角度注入器找不到该服务 错误:Angular 角度2喷油器can';加载CustomExceptionHandler时找不到服务,angular,angular2-injection,Angular,Angular2 Injection,尝试将服务注入CustomExceptionHandler时,角度注入器找不到该服务 错误:Uncaught无法解析CustomExceptionHandler:(?)的所有参数。 设置: CustomExceptionHandler import { ExceptionHandler } from '@angular/core'; import { ServiceA } from '../services/a.service'; export class CustomExceptionHa
Uncaught无法解析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 { }
您还需要将
CustomExceptionHandler
及其依赖项添加到提供者
:
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
ServiceA,
CustomExceptionHandler,
{ provide: ExceptionHandler, useClass: CustomExceptionHandler },
],
bootstrap: [AppComponent],
})
export class AppModule { }
更新
异常处理程序
被重命名为ErrorHandler
原始版本
将@Injectable()
添加到CustomExceptionHandler类
将CustomExceptionHandler添加为app.module.ts中的另一个提供商:
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
ServiceA,
CustomExceptionHandler,
{ provide: ExceptionHandler, useClass: CustomExceptionHandler },
],
bootstrap: [AppComponent],
})
export class AppModule { }
我已修改错误:
Uncaught无法解析CustomExceptionHandler:(?)的所有参数。
不是这样,问题仍然存在。谢谢你的时间,谢谢冈特和艾德的回答。正是这两者的结合修复了它。您需要添加@Injectable(),并且除了将其作为useClass列出以覆盖ExceptionHandler之外,还需要在app.module.ts中将其作为普通提供程序列出。这给我留下了一个困境,让谁来接受这个答案呢。无论谁输入这些组合,都会得到它。再次感谢!每个人都必须从某个地方开始。我也不是从10万开始;-)<代码>从“../services/a.Service”导入{Service}可能应该是从“../services/a.service”导入{ServiceA}代码>(服务名称中缺少A
)感谢Gunter和ade的回答。正是这两者的结合修复了它。您需要添加@Injectable(),并且除了将其作为useClass列出以覆盖ExceptionHandler之外,还需要在app.module.ts中将其作为普通提供程序列出。这给我留下了一个困境,让谁来接受这个答案呢。无论谁输入这些组合,都会得到它。再次感谢!