简体   繁体   English

在ngrx中订阅内管道

[英]Piping inside a subscribe in ngrx

I have a selector that takes a parameter to filter values. 我有一个选择器,它带有一个参数来过滤值。

The parameter depends on an observable's 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 }));
});
}

the selectSchedulingsTimes selector is defined as well: 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
    });
  }
);

in a second time I subscribe to schedules$ 我第二次订阅schedules$

this.schedules$.subscribe((schedules: ISchedule[]) => {

  ...

}

I get inside selectSchedulingsTimes selector only once when application is started, but when I change values of selectedDate$ the selector is not triggered so I'm not entering in selectSchedulingsTimes . 当应用程序启动时,我仅进入一次selectSchedulingsTimes选择器,但是当我更改selectedDate$的值时,不会触发选择器,因此我不会输入selectSchedulingsTimes

How can I make selector send new data every change on selectedDate ? 如何使选择器在selectedDate上的每次更改都发送新数据?

Is it because I'm not subscribing to schedules$ ? 是因为我没有订阅schedules$吗?

Don't hesitate to ask me for unclear parts 不要犹豫,问我不清楚的部分

Let's change your observable setup like this: 让我们这样更改您的可观察设置:

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);      
    })    
  }

You need not subscribe on selectedData$ and then set up your other observable. 您无需订阅selectedData$ ,然后设置其他可观察对象。 Hope it helps. 希望能帮助到你。

I think your selector is the problem. 我认为您的选择器是问题所在。 You should read this , its giving some good example and it also contains a section regarding dynamic parameters. 您应该阅读此书 ,它提供了一个很好的示例,并且还包含有关动态参数的部分。 Thats what I would try: 那就是我会尝试的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM