【发布时间】:2018-11-15 06:36:49
【问题描述】:
我正在使用带有 NGRX 效果的 Angular,而且我是 observables 世界的新手。我想检查我的商店中是否存在数据,那么不应该调用 API,并且应该从商店中提取数据。我设法发现我可以使用 withLatestFrom() 来检查商店中的最新值。下一部分让我感到困惑,我无法让它工作。我当前的代码如下:
@Effect() getSomeContent$ = this.actions$
.ofType(GET_SOME_CONTENT)
.withLatestFrom(this.store$.select('store-section'))
.map(([action, store]) => {
const content = {};
if (store.someContent.length > 0) {
return new GetCategoryContentSuccessAction({ categoryContent: categories });
}
return Observable.from(action.payload);
})
.switchMap(payload => this.APIcall(payload)
.then(data => {
const content = {};
data.items.map((item) => {
const slug = data.slug;
content[slug] = {
info: datasomeData
};
});
return new GetSomeContentSuccessAction({ categoryContent: categories });
})
.catch((err) => {
return new GetFailureAction({ error: {} });
})
);
我想使用某种 if 或 else 语句来检查商店。如果数据存在,我想为我必须处理的减速器发回一个空对象。如果不是,我想调用 API 并将该数据发回。我不知道 observable 是否可以换句话说可以分支所以可以做到这一点?
有没有更好的方法可以实现这一目标?可能通过创建另一个操作来单独处理 API。我想了解未来的最佳实践。任何帮助将不胜感激。
【问题讨论】:
标签: angular observable ngrx-store ngrx-effects