[英]Difference between EventEmitter.next() and EventEmitter.emit() in Angular 2
What is the difference between EventEmitter.emit()
and EventEmitter.next()
? EventEmitter.emit()
和EventEmitter.next()
什么EventEmitter.next()
? Both dispatching the event to the subscribed listeners.两者都将事件分派给订阅的侦听器。
export class MyService {
@Output() someEvent$: EventEmitter<any> = new EventEmitter();
someFunc() {
this.someEvent$.emit({myObj: true});
this.someEvent$.next({myObj: true});
}
}
The documenation for the EventEmitter is not so helpful at the moment. EventEmitter 的文档目前不是很有帮助。
They do the same.他们做同样的事情。
emit()
is the current version, next()
is deprecated. emit()
是当前版本, next()
已弃用。
See also https://github.com/angular/angular/blob/b5b6ece65a96f5b8f134ad4899b56bf84afe3ba0/modules/angular2/src/facade/async.dart#L49另见https://github.com/angular/angular/blob/b5b6ece65a96f5b8f134ad4899b56bf84afe3ba0/modules/angular2/src/facade/async.dart#L49
In the latest version(Ng9), the source code of event_emitter.ts
goes as following:在最新的版本(NG9)的源代码
event_emitter.ts
去如下:
export class EventEmitter<T extends any> extends Subject<T> {
/**
* Emits an event containing a given value.
* @param value The value to emit.
*/
emit(value?: T) { super.next(value); }
}
EventEmitter
extends from parent class Subject
. EventEmitter
自父类Subject
。 And emit
method call super.next()
as you may expected.并如您所料,
emit
方法调用super.next()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.