Imagine having an Angular 2 setup with a root parent component A
, two distinct children components B
and C
:
https://i.sstatic.net/w9bOf.jpg
Now, if there is a need to create a shared service called MyService
that can be injected into both child components vertically, then one approach would involve wrapping each column of child components in an intermediate component P
. By declaring MyService
as a provider in the @Component
decorator of P
, Angular will automatically create separate instances of the service for each column:
https://i.sstatic.net/TlLTG.jpg
However, this method adds unnecessary complexity by introducing extra boilerplate code and requiring changes in how components are structured within the template of component A
.
Is there a simpler way to accomplish this task in Angular 2 without resorting to intermediary wrapper components? Your suggestions are greatly appreciated!