[英]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.