我需要在 BehaviorSubject 中添加项目。
我需要在每个对象中添加带有名称的项目(项目名称已扩展) 我的尝试:
答案 0 :(得分:0)
类似的东西?
this.someService.getList()
.subscribe(dates => {
const fixedDates = dates.map(el => ({...el, expanded: false}));
this.dates.next(fixedDates)
});
答案 1 :(得分:0)
我的理解是您想在 BehaviorSubject 的值数组中添加 expanded 属性。
这就是您的操作方式,BehaviorSubject 具有 value
属性,其中包含最后发出的数据。
this.dates.value.forEach(val => (val.expanded = true));
console.log(this.dates.value);
注意:先修改数据然后发出下一个值(如@Bojan Kogoj所示),否则现有订阅者将无法获得最新修改的数据。
答案 2 :(得分:0)
我在服务中通常这样做:
private refresh$ = new BehaviorSubject(null);
read$(): Observable<Order[]>{
return this.refresh$.pipe(switchMap(()=>
this.http.get<Order[]>(`${this._url}/orders`)));
}
答案 3 :(得分:0)
如果你想为数组中的每个对象添加 'expanded' 属性,请执行以下操作:
private datesSource: BehaviorSubject<any> = new BehaviorSubject<any>(this.test);
public datesObservable$: Observable<any> = this.datesSource.asObservable();
this.datesSource.next(response); // Set your dates object and subscribe to the observale
this.datesObservable$.subscribe(datesObsdata => {
datesObsdata.forEach(eachObj => {
if (eachObj && !eachObj['expanded']) {
eachObj['expanded'] = false;
}
})
})