Search code examples
angulardependency-injectionangular2-services

Bundling of services in Angular 2


I have a question regarding the best practices for the following situation:

We have multiple components in the same module which use the same of multiple services. Is there a way around injecting each service into each component?

So far I went with a BaseComponent into which I injected each service needed, and which all other components extended to get access to those services.

Is this considered good practice? Is there another way?

Thanks in advance!


Solution

  • I would not recommend extending your Components, as this feels a bit clunky, although technically it would work.

    You can however create a BaseService, which injects some other services, and only inject the BaseService in the components. That way, you can create a sort of hierarchy of services.

    Keep in mind that injecting is there for a good reason, and you should only inject the services you really need in any component.