How to solve the circular dependency

前端 未结 2 849
忘了有多久
忘了有多久 2020-12-01 16:44

I have 3 services:

auth.service.ts, account.service.ts, http.service.ts

While user signup I should create new account therefore

2条回答
  •  鱼传尺愫
    2020-12-01 17:13

    You can use Injector for this. Inject it via constructor as usual, and then when you will need some service that leads to the circular dependency, get that service from it.

    class HttpService {
      constructor(private injector: Injector) { }
    
      doSomething() {
        const auth = this.injector.get(AuthService);
        // use auth as usual
      }
    }
    

提交回复
热议问题