How to solve the circular dependency

前端 未结 2 832
忘了有多久
忘了有多久 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
      }
    }
    
    0 讨论(0)
  • 2020-12-01 17:15

    @deprecated from v4.0.0 use Type or InjectionToken

    const auth = this.injector.get(AuthService);
    

    Works on Angular 10:

    const AuthService = this.injector.get<AuthService>(AuthService);
    
    0 讨论(0)
提交回复
热议问题