在ngrx的订阅中管道传递 [英] Piping inside a subscribe in ngrx

查看:70
本文介绍了在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 }));
});
}

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.

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.

如何使选择器在selectedDate上的每次更改都发送新数据?

How can I make selector send new data every change on selectedDate?

是因为我没有订阅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);      
    })    
  }

您无需订阅selectedData$,然后设置其他可观察的对象. 希望对您有所帮助.

You need not subscribe on selectedData$ and then set up your other observable. Hope it helps.

这篇关于在ngrx的订阅中管道传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆