RxJs: Incrementally push stream of data to BehaviorSubject<[]>

*爱你&永不变心* 提交于 2019-12-05 00:21:36

You can use getValue() method to achieve what you want to do.

Example:

data = new BehaviorSubject<any[]>([]);

addData(foo:any):void{
  // I'm using concat here to avoid using an intermediate array (push doesn't return the result array, concat does).
  this.data.next(this.data.getValue().concat([foo]));
}

Instead of using concat, you can instead use the spread operator:

data = new BehaviorSubject<any[]>([]);

addData(foo: any): void {
  this.data.next([...this.data.getValue(), ...foo])
}

I've found this to be a bit more readable than a straight concat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!