You can solve this problem using the service search service. This will easily allow you to get any service and use it in your parent classes without introducing them through your children (this can be a problem).
To use this, create a simple locator.service.ts class:
import {Injector} from "@angular/core"; export class ServiceLocator { static injector: Injector; }
Import this service into your app.module.ts :
import {ServiceLocator} from './locator.service';
Then, in the constructor file of your module ( app.module.ts ?), Run this thing so that it can be used anywhere:
export class AppModule { constructor(private injector: Injector){
Now, to use it in your superclass (your BaseComponent), just import the ServiceLocator
import {ServiceLocator} from './locator.service';
and use it like:
export class BaseComponent { public loader; constructor() { this.loader = ServiceLocator.injector.get(LoadingService) } showLoading() { this.loader.show(); } }
Now you have embedded your service in an extensible parent element, and you can use it in child components without passing it to super ().
Mikeone
source share