Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper use of an EventEmitter?

I've read questions like Access EventEmitter Service inside of CustomHttp where the user uses EventEmitter in his service, but he was suggested in this comment not to use it and to use instead Observables directly in his services.

I also read this question where the solution suggests to pass the EventEmitter to the child and subscribe to it.

My question then is: Should I, or should I not subscribe manually to an EventEmitter? How should I use it?

like image 666
Eric Martinez Avatar asked Mar 18 '16 05:03

Eric Martinez


People also ask

What is the use EventEmitter?

EventEmitter is a module that helps share data between components using emit() and subscribe() methods. EventEmitter is in the Observables layer, which observes changes and values and emits the data to the components subscribed to that EventEmitter instance.

How does EventEmitter work Angular?

๐ŸŽŠ Event Emitters in Angular ๐ŸŽŠData flows into your component via property bindings and flows out of your component through event bindings. If you want your component to notify his parent about something you can use the Output decorator with EventEmitter to create a custom event.

Should I use EventEmitter or subject?

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


1 Answers

TL;DR:

No, don't subscribe manually to them, don't use them in services. Use them as is shown in the documentation only to emit events in components. Don't defeat angular's abstraction.

Answer:

No, you should not subscribe manually to it.

EventEmitter is an angular2 abstraction and its only purpose is to emit events in components. Quoting a comment from Rob Wormald

[...] EventEmitter is really an Angular abstraction, and should be used pretty much only for emitting custom Events in components. Otherwise, just use Rx as if it was any other library.

This is stated really clear in EventEmitter's documentation.

Use by directives and components to emit custom Events.

What's wrong about using it?

Angular2 will never guarantee us that EventEmitter will continue being an Observable. So that means refactoring our code if it changes. The only API we must access is its emit() method. We should never subscribe manually to an EventEmitter.

All the stated above is more clear in this Ward Bell's comment (recommended to read the article, and the answer to that comment). Quoting for reference

Do NOT count on EventEmitter continuing to be an Observable!

Do NOT count on those Observable operators being there in the future!

These will be deprecated soon and probably removed before release.

Use EventEmitter only for event binding between a child and parent component. Do not subscribe to it. Do not call any of those methods. Only call eve.emit()

His comment is in line with Rob's comment long time ago.

So, how to use it properly?

Simply use it to emit events from your component. Take a look a the following example.

@Component({     selector : 'child',     template : `         <button (click)="sendNotification()">Notify my parent!</button>     ` }) class Child {     @Output() notifyParent: EventEmitter<any> = new EventEmitter();     sendNotification() {         this.notifyParent.emit('Some value to send to the parent');     } }  @Component({     selector : 'parent',     template : `         <child (notifyParent)="getNotification($event)"></child>     ` }) class Parent {     getNotification(evt) {         // Do something with the notification (evt) sent by the child!     } } 

How not to use it?

class MyService {     @Output() myServiceEvent : EventEmitter<any> = new EventEmitter(); } 

Stop right there... you're already wrong...

Hopefully these two simple examples will clarify EventEmitter's proper usage.

like image 151
Eric Martinez Avatar answered Oct 14 '22 07:10

Eric Martinez