To prevent the need to continuously pass either the injector or the injected instance from the child class to the parent class, consider utilizing the following alternative approach.
Start by creating a class with a single static property:
export class AppServices {
static injector: Injector;
}
Next, assign a value to the injector within the module where your service is initialized:
import {Injector, NgModule} from '@angular/core';
@NgModule({
providers: MyService
})
export class AppModule {
constructor(private injector: Injector) {
AppServices.injector = this.injector;
}
}
Once the module has been instantiated, you can utilize the static injector in any class to access any of the module's services:
export class ParentComponent {
private myService: MyService;
constructor () {
this.myService = AppServices.get<MyService>(MyService);
}
}