When we start to use any new tools it is important to know why are we using it and where to use it. Likewise, it is important to understand the difference between Observable, EventEmitter, and Subject and where to use it. It always gets confusing to choose between Eventemitter and Subject for transferring data from one component to another component.

What is observable?

Angular uses observables as an interface to handle a variety of common asynchronous operations. We can also say it as a data source that will emit data over a period of time. It’ll start to emit data asynchronously when it is subscribed.

What is EventEmitter?

It is used with @Output directive to emit custom event synchronously or asynchronously using emit() method. Using EventEmitter we can transfer data from child component to parent component. It is true, we can transfer data from one component to another using EventEmitter but it is not recommended.

What is Subject?

It is a special type of observable that acts as both observable and observer. It’ll emit new value using next() method. All the subscribers who subscribe to the subject when emitted will receive the same value.

Which one to use for transferring data from one component to another, EventEmitter or Subject?

It is recommended to use Subject for transferring data from one component to another as EventEmitter extends Subject, adding a method emit().

Conclusion

  • Use Eventemitter when transferring data from child component to parent component.
  • Use Subject to transfer data from one component to another component.