【发布时间】:2018-01-06 06:54:26
【问题描述】:
我有一个显示加载微调器的数据表。但是,当 this.rendering 设置为 true 时,加载程序会显示 - 如果从 this._CollectionDataSet.results 返回的结果是一个空数组,则序列会卡在 switchMap 并且无法完成 - 为什么会这样?
数据源.ts
connect(): Observable<Collection[]> {
// Listen for any changes in the base data, sorting, filtering, or pagination
const displayDataChanges = [
this._CollectionDataSet.dataChange,
this._sort.sortChange
];
return Observable.merge(...displayDataChanges)
.startWith(null)
.switchMap(() => {
this.rendering = true;
return this._CollectionDataSet.dataChange;
})
.catch(() => {
return Observable.of(null);
})
.map((result) => {
this.rendering = false;
return result;
})
.map(result => {
if (!result) { return []; }
this._recordsTotal = result.length;
const sortedData = this.sortData(result.slice());
this.renderedData = sortedData;
return this.renderedData;
});
}
数据集.ts
dataChange: BehaviorSubject<Collection[]> = new BehaviorSubject<Collection[]>([]);
get results(): Collection[] { return this.dataChange.value; }
constructor(
private _collectionService: CollectionService,
private route: ActivatedRoute,
private router: Router
) {
this.route.params.subscribe((param: any) => {
this._collectionService.view(param.id).subscribe((results) => this.update(results));
});
}
update(results: Collection[]) {
results && this.dataChange.next(results);
}
【问题讨论】:
标签: typescript rxjs observable