[英]Piping inside a subscribe in ngrx
我有一個選擇器,它帶有一個參數來過濾值。
該參數取決於可觀察對象的返回。
export class LineSynopticComponent implements OnInit, AfterViewInit {
schedules$: Observable<ISchedule[]>;
ngOninit(){
this.selectedDate$.subscribe(elm => {
this.schedules$ = this.store.pipe(select(selectSchedulingsTimes, { time: elm.timeSelect }));
});
}
selectSchedulingsTimes
選擇器也已定義:
const schedulings = (state: IAppState) => state.schedulings;
export const selectSchedulingsTimes = createSelector(
schedulings,
(state: ISchedulesState, { time }: { time: string }) => {
let nowFormat = moment(time, 'HH:mm');
return state.schedulings.data.filter(elm => {
...
return elm
});
}
);
我第二次訂閱schedules$
this.schedules$.subscribe((schedules: ISchedule[]) => {
...
}
當應用程序啟動時,我僅進入一次selectSchedulingsTimes
選擇器,但是當我更改selectedDate$
的值時,不會觸發選擇器,因此我不會輸入selectSchedulingsTimes
。
如何使選擇器在selectedDate
上的每次更改都發送新數據?
是因為我沒有訂閱schedules$
嗎?
不要猶豫,問我不清楚的部分
讓我們這樣更改您的可觀察設置:
ngOninit(){
this.selectedData$.pipe(
switchMap((elm) => {
return this.store.pipe(select(selectSchedulingsTimes, { time: elm.timeSelect }));
})
).subscribe((resposeFromStore) => {
//Do whatever you want tot do with the store value
console.log(resposeFromStore);
})
}
您無需訂閱selectedData$
,然后設置其他可觀察對象。 希望能幫助到你。
我認為您的選擇器是問題所在。 您應該閱讀此書 ,它提供了一個很好的示例,並且還包含有關動態參數的部分。 那就是我會嘗試的:
export const selectSchedulingsTimes = createSelector(
schedulings,
schedule => (time: string) => {
let nowFormat = moment(time, 'HH:mm');
return state.schedulings.data.filter(elm => {
...
return elm
});
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.