Angular 2中EventEmitter.next()和EventEmitter.emit()之间的区别

Hol*_*itz 81 angular2-services angular

EventEmitter.emit()和之间有什么区别EventEmitter.next()?两者都将事件分派给订阅的侦听器.

export class MyService {
  @Output() someEvent$: EventEmitter<any> = new EventEmitter();

  someFunc() {
   this.someEvent$.emit({myObj: true});

   this.someEvent$.next({myObj: true});
  }
}
Run Code Online (Sandbox Code Playgroud)

为EventEmitter documenation是此刻不那么有用.

Gün*_*uer 100

他们做同样的事情.emit()是当前版本,next()已弃用.

另见https://github.com/angular/angular/blob/b5b6ece65a96f5b8f134ad4899b56bf84afe3ba0/modules/angular2/src/facade/async.dart#L49

  • 很高兴知道。谢谢。 (3认同)

Chr*_*Bao 8

在最新版本(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); }
}

Run Code Online (Sandbox Code Playgroud)

EventEmitter从父类扩展Subject。和emit方法调用,super.next()如您所料。