Angular RxJs:递增地将数据流推送到行为主体<;[]>;

Angular RxJs:递增地将数据流推送到行为主体<;[]>;,angular,rxjs5,Angular,Rxjs5,基本上,我正试图用数据数组来膨胀行为主体,这些数据将以块的形式加载 BehaviorSubject将添加新的数据块(如Array.push),但我不想使用另一个数组来存储和添加到BehaviorSubject.next,因为随着数据的增长,这将导致渲染时间过长 有什么建议吗?您可以使用getValue()方法来实现您想要做的事情 例如: data = new BehaviorSubject<any[]>([]); addData(foo:any):void{ // I'm us

基本上,我正试图用数据数组来膨胀
行为主体
,这些数据将以块的形式加载

BehaviorSubject
将添加新的数据块(如
Array.push
),但我不想使用另一个数组来存储和添加到
BehaviorSubject.next
,因为随着数据的增长,这将导致渲染时间过长

有什么建议吗?

您可以使用
getValue()
方法来实现您想要做的事情

例如:

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]));
}
data=newbehaviorsubject([]);
addData(foo:any):无效{
//我在这里使用concat是为了避免使用中间数组(push不会返回结果数组,concat会)。
this.data.next(this.data.getValue().concat([foo]);
}

您可以使用spread操作符代替
concat

data=newbehaviorsubject([]);
addData(foo:any):无效{
this.data.next([…this.data.getValue(),…foo])
}
我发现这比我今天所用的纯
concat

getValue()
更具可读性。谢谢