Angular 2 Event emitters vs Subject

前端 未结 3 2006
Happy的楠姐
Happy的楠姐 2020-12-04 20:46

In Angular 2 what is the difference between Event Emitter and Subject for announcing an event? It seems like event emitters are less complicated to declare....Which way is p

相关标签:
3条回答
  • 2020-12-04 21:36

    EventEmitter by default is synchronous, whereas Subject is not. You can pass a flag to EventEmitter to make it asynchronous.

    0 讨论(0)
  • 2020-12-04 21:41

    Also EventEmitter gets cleaned up automatically unlike custom Subjects that you need to unsubscribe to in the onDestroy lifecycle hook.

    0 讨论(0)
  • 2020-12-04 21:46

    There is not much difference. EventEmitter extends Subject.

    The Angular2 team stressed the fact though, that EventEmitter should not be used for anything else then @Output()s in components and directives.

    The Angular team has stated that they may change the underlying implementation of EventEmitter and break user code that uses EventEmitter for something it wasn't intended for. That's the main difference.

    0 讨论(0)
提交回复
热议问题