RxJs中的Subject和Angular2中的EventEmitter
Subject in RxJs and EventEmitter in Angular2
它们之间有什么区别,何时以及如何使用它们?我读到Subject相当于EventEmitter。
如果我想重写这个,怎么写?
import { Injectable} from '@angular/core';
import { Subject,BehaviorSubject } from 'rxjs';
import {Playlists} from 'channel' /** Assumes this is where you have defined your Playlists interface **/
@Injectable()
export class PlaylistService {
private _currentPlaylists$: Subject<Playlists> = new BehaviorSubject<Playlists>(null);
constructor() {}
currentPlaylists() {
return this._currentPlaylists$.asObservable();
}
setCurrentPlaylists(playlists:Playlists){
this._currentPlaylists$.next(playlists);
}
}
EventEmitter
s应该只在使用Output
装饰器在Angular2组件中实现自定义事件时使用:
@Output()
someEvent: EventEmitter = new EventEmitter();
在其他情况下,你可以使用Subject
s(来自Rxjs),因为它与Angular2的特定功能无关。
EventEmitter
内部扩展Subject
。看到https://github.com/angular/angular/blob/master/modules/%40angular/facade/src/async.ts L63
你的代码看起来不错;-)
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 可以't让我的if语句处理js中的html表单输入
- 引用对象中的通用值
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 不能从angular2中的子组件指定父组件中的数组
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如何更改bigquery API中的计费层选项
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 从Node.js中的EventEmitter继承的原型数组
- 响应式编程——RxJS与Node.js中的EventEmitter
- RxJs中的Subject和Angular2中的EventEmitter