angulardependency-injectionextending

How to inject service in static class without passing that in children class in angular 7?


I have this abstract class as component base:

protected constructor(
    protected model: Type<T>,
    protected activatedRoute: ActivatedRoute,
    protected router: Router,
    protected service: CrudServiceBase<T>,
    private ng7MatBreadcrumbService :Ng7MatBreadcrumbService
  ) {}

ng7MatBreadcrumbService is not depend on components but i have to passing this service in each children components! Is there any way to using ng7MatBreadcrumbService without passing that in each children components?


Solution

  • You need to inject it in every component that uses it. Decorator @Injectable() means that you need to inject it for use. Service itself means that it should be injected, you just choose type of the injection.

    More about it here: https://angular.io/guide/dependency-injection