Search code examples
angularobservableeventemitter

EventEmitter vs SharedService when communicating 2 childrenComponents. Angular


I have an Angular app where I have One parent component and two children components. One of the childrens have some data that the other children needs to be rendered, etc. I implemented a shared service with an Observable where children one updated the data and children two subscribes to the observable using the service and it was working good.

But then I discussed my implementation with my boss and he didn0t like the sharedService implementation, so he suggest that I communicate the children through the parent component and using EventEmitter (@Input, @Output) just as the image:

enter image description here

The thing is, that before I do any changes I would like to know witch implementation is better in terms of good practices and performance. I apprecciate any help.


Solution

  • According to me Observable is always better to communicate in your case. The disadvantage with event emitter is if a new component is added between child and the existing parent then again you need to change the code. This is not the problem with Observable service.

    With Observable service you are making your component to more reactive in nature. It like Pull vs Push model. With Event emitter your are pushing the change and with Observable its always a pull.