How to solve the circular dependency
Just wanted to share a scenario that happened with me (maybe it could help someone). So I was getting the same circular dependency error and a Null Injector error too. The only thing that made my head spin was that I just had a single service and there was no literal circular dependency.
So I just tried to look the Null Injector error, the issue was that in my app.module
I hadn't imported HttpClientModule
. Once I imported that in imports array both error got fixed.
@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);
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
}
}