Angular 2事件发射器与主题 [英] Angular 2 Event emitters vs Subject
问题描述
在Angular 2中,事件发射器和主题之间宣布事件的区别是什么?似乎事件发射器的声明并不那么复杂....Angular 2首选哪种方式?
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 preferred by Angular 2?
dataRefreshEvent = new EventEmitter();
private companyDataAnnouncedSource = new Subject();
companyDataAnnouncedSource$ = this.companyDataAnnouncedSource.asObservable();
推荐答案
没有太大区别. EventEmitter
扩展Subject
.
There is not much difference. EventEmitter
extends Subject
.
但是Angular2团队强调了一个事实,除了组件和指令中的@Output()
之外,EventEmitter
不得用于其他任何用途.
The Angular2 team stressed the fact though, that EventEmitter
should not be used for anything else then @Output()
s in components and directives.
Angular团队已声明,他们可能会更改EventEmitter
的基础实现,并破坏使用EventEmitter
的用户代码,这些代码本来是不希望的.那是主要的区别.
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.
这篇关于Angular 2事件发射器与主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!