Angular语言 - 检查observable是否被加载



我正在调用一个可观察对象,这需要一些时间来解决,我添加了一个条件来检查我们是否得到一个有效的结果(它工作得很好,但在我看来,不应该这样做)。

代码:

this.store.select(state => this.list = state.list)
.subscribe(result => {
//Without checking if result exists, it throws here undefined, only solution found so far is to add the check below
if (result) {
console.log('result is loaded');
this.copyOfList = [...this.list];
for (const item of result) {
this.itemCategories(item.category);
}
}
});

可以使用skipWhile:

this.store.select(state => this.list = state.list).pipe(skipWhile(x => !!x))
.subscribe(result => {
});

我更喜欢filter

this.store.select(state => this.list = state.list)
.pipe(filter(x => !!x))
.subscribe();

我更喜欢这个,主要是因为数组的相似性。过滤方法。