在 ngrx 中的订阅内管道

Piping inside a subscribe in ngrx

我有一个使用参数来过滤值的选择器。

参数取决于 observable 的 return。

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$ 吗?

如有不明之处,请随时向我询问

我认为您的选择器有问题。您应该阅读 this,它提供了一些很好的示例,并且还包含有关动态参数的部分。那就是我会尝试的:

export const selectSchedulingsTimes = createSelector(
  schedulings,
  schedule => (time: string) => {
    let nowFormat = moment(time, 'HH:mm');
    return state.schedulings.data.filter(elm => {
      ... 
      return elm
    });
  }
);

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

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$ 然后设置您的其他可观察对象。 希望对你有帮助。