为另一个服务注入服务
Injecting service for another service
我有几个相互调用的服务级别,即ComponentService
使用DataService
使用ErrorService
。
现在,其中一个组件服务需要指定DataService
使用的ErrorService
实例应该是例如SpecialErrorService
类型。-它不直接使用ErrorService
或SpecialErrorService
。
我如何指示数据服务在实例化时注入正确的ErrorService
类型?(注意,我希望这种行为只针对特定的ComponentService
,而不是一般情况)
@Injectable()
export class SpecialComponentService {
constructor(private _dataService: DataService){}
log() {
//expecting this to log: I'm a special error service
this._dataService.log();
}
}
@Injectable()
export class RegularComponentService {
constructor(private _dataService: DataService){}
log() {
//expecting this to log: I'm a regularerror service
this._dataService.log();
}
}
@Injectable()
export class DataService {
constructor(private _errorService: ErrorService){}
log() {
this._errorService.log();
}
}
@Injectable()
export class ErrorService {
log(){
console.log("I'm a regular error service");
}
}
@Injectable()
export class SpecialErrorService extends ErrorService {
log(){
console.log("I'm a special error service");
}
}
提供服务时可以用特殊服务代替。例如,如果您在组件中使用它,您可以像这样提供它:
{提供:ErrorService, useClass: SpecialErrorService}
PS:我想把这个作为评论发布,但我不被允许。
相关文章:
- AngularJS在不使用变量的情况下将服务注入模块中,这样就可以缩小它
- AngularJS 服务注入器错误
- 采用多种方法在控制器中进行角服务注入
- 是否可以将服务注入Angular 1.5组件's templateUrl属性
- 当服务注入到其他地方时,角度提供程序配置重置
- 如何将服务注入链接函数包含指令
- 将控制器作为打字稿/角度中的服务注入
- 无法将服务注入其 Angular 单元测试中
- 将服务注入指令 AngularJS 和数据绑定
- 为什么当服务注入第二个控制器时,AngularJS 1.4.8 应用程序不起作用
- 将升级的服务注入组件
- 错误:将Angular$http服务注入需要插件
- 如何将多个服务注入一个控制器
- AngularJS,将服务注入控制器
- Angular如何将服务注入.run(函数
- 如何将服务注入AngularJS'的UI路由器templateUrl函数
- 当将服务注入(导入)到我的构造函数中时;无提供者”;错误
- 在AngularJS中,当自定义服务注入到不同的模块时,未知的提供商
- 为另一个服务注入服务
- 为什么我不能把服务注入到Angular的工厂/注入器中?