Sak*_*uto 20 typescript angular
我正在尝试实现ConfigService以检索项目中正确环境的正确配置.我目前正在遇到循环依赖
(index):28 Error: (SystemJS) Provider parse errors:
Cannot instantiate cyclic dependency! Http: in NgModule AppModule
Error: Provider parse errors:
Run Code Online (Sandbox Code Playgroud)
在我看来,我已经探索过代码并且存在问题:
CustomHttp
constructor(backend: XHRBackend, options: RequestOptions, public spinnerService: SpinnerService, public exceptionService: ExceptionService, public configService: ConfigService)
Run Code Online (Sandbox Code Playgroud)
ExceptionService
constructor(private _notificationService: NotificationService, private _spinnerService: SpinnerService, private _configService: ConfigService, private _router: Router)
Run Code Online (Sandbox Code Playgroud)
的ConfigService
constructor(private http: Http) {}
Run Code Online (Sandbox Code Playgroud)
如您所见,我在此图中说明了一个循环依赖(没有任何好的约定):
我现在的问题是,如何修复它?我听说过,Injector但我不确定我是否真的可以在这种情况下使用它.
提前感谢您的回答.
Gün*_*uer 28
DI无法解决循环依赖关系.解决方法是注入注入器并强制获取实例:
@Injectable()
class ConfigService {
private http: Http;
constructor(injector:Injector) {
setTimeout(() => this.http = injector.get(Http);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6699 次 |
| 最近记录: |