Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2喷油器can';加载CustomExceptionHandler时找不到服务_Angular_Angular2 Injection - Fatal编程技术网

Angular 角度2喷油器can';加载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

尝试将服务注入CustomExceptionHandler时,角度注入器找不到该服务

错误:
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中将其作为普通提供程序列出。这给我留下了一个困境,让谁来接受这个答案呢。无论谁输入这些组合,都会得到它。再次感谢!