DI with cyclic dependency with custom HTTP and ConfigService

前端 未结 1 2002
栀梦
栀梦 2020-11-27 07:23

I\'m trying to implement a ConfigService to retrieve the right configuration for the right environment in the project. I\'m currently encountering a cyclic dependancy

<
相关标签:
1条回答
  • 2020-11-27 08:01

    DI can't resolve cyclic dependencies. A workaround is to inject the injector and acquire the instance imperatively:

    @Injectable()
    class ConfigService {
      private http: Http;
      constructor(injector:Injector) {
        setTimeout(() => this.http = injector.get(Http);
      }
    }
    
    0 讨论(0)
提交回复
热议问题