首页 > 解决方案 > 在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$吗?

不要犹豫,问我不清楚的部分

标签: javascriptreduxrxjsstorengrx

解决方案


让我们像这样更改您的可观察设置:

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$然后设置你的其他 observable。希望能帮助到你。


推荐阅读