Angular - 检查是否加载了 observable
Angular - Check if observable is loaded or not
我正在调用一个需要一些时间才能解决的可观察对象,我添加了一个条件来检查我们是否得到了有效结果(它工作正常但在我看来不应该这样做).
代码:
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();
我更喜欢这个主要是因为 array.filter 方法的相似性。
我正在调用一个需要一些时间才能解决的可观察对象,我添加了一个条件来检查我们是否得到了有效结果(它工作正常但在我看来不应该这样做).
代码:
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();
我更喜欢这个主要是因为 array.filter 方法的相似性。