此代码的主要目的是检查存储中是否存在值,并防止对服务器的额外请求。整个逻辑工作正常,但不幸的是,当过滤器返回错误的选择器时,整个选择逻辑都不会应用于可观察的对象,即使值存储在数组中也为空。
this.events$ = this.paginationService.page$.pipe(
switchMap(pageIndex => this.store.pipe(
delay(0),
//get objects from store
select(selectEventsPageByGenre(this.musicGenre, { pageIndex: pageIndex, pageSize: 3 })),
//if there are no objects from select() the dispatch server request
filter(events => events.length === 0),
map(() => {
this.store.dispatch(new EventsPageRequested({
musicGenre: MusicGenre[this.musicGenre.toUpperCase()],
page: {
pageIndex: pageIndex,
pageSize: 6
}
}));
return [];
})
))
);
当过滤器返回false(如果数组不为空)时如何从选择器返回值?
预先感谢
答案 0 :(得分:1)
只需删除filter()
,然后将map()
替换为
map(() => {
if (events.length > 0) {
return events;
}
this.store.dispatch(new EventsPageRequested({
musicGenre: MusicGenre[this.musicGenre.toUpperCase()],
page: {
pageIndex: pageIndex,
pageSize: 6
}
}));
return [];
})