I’m running into a situation where I can’t seem to use transient service that injects other transient services. For example, suppose I have a services that looks like this:
@Injectable()
export class ParentService {
private child1 = inject(ChildService1);
private child2 = inject(ChildService2);
// ... more injects
}
@Injectable()
export class ChildService1 {
private subChild1 = inject(SubChildService1);
// ... more injects
}
@Injectable()
export class ChildService2 {
private subChild2 = inject(SubChildService2);
// ... more injects
}
@Injectable()
export class SubChildService1 {
}
@Injectable()
export class SubChildService2 {
}
Now, I would like to use ParentService in my component like this:
@Component({
providers: [ParentService]
})
export class MyComponent {
private parentService = inject(ParentService);
}
If I don’t list all my services, including services that ParentService depends on, in providers
array, I get NullInjectionError. The problem is that I could have lots of services injected in ParentService that also inject other transient sub services. How can I just use transient ParentService without listing all dependencies in providers array?