【问题标题】:Properly select piece of state from store从商店中正确选择状态
【发布时间】:2019-11-07 16:21:25
【问题描述】:

我想订阅 ngrx 存储中的一个状态,并仅在该特定状态发生更改时接收排放。

export const getMatchesState = (state: State) => state.matches;

export const getSpainMatches = createSelector(getMatchesState, (allMatches) => {
  return Object.keys(allMatches.byId)
    .filter(key => {
      return allMatches.byId[key].tournamentId == '68';
    }).map(key => allMatches.byId[key]);
});

当我订阅这个likeso时

this.testSub = this.store.pipe(select(fromRoot.getSpainMatches))
      .subscribe(s => console.log('@@@@', s));

每次更改商店中的任何匹配项时都会产生排放,而不仅仅是西班牙匹配项更改时。

更新:

我也试过这个:

export const getSpainMatches = createSelector(getMatchesState, (allMatches, props) => {
  return Object.keys(allMatches.byId)
    .filter(key => {
      return allMatches.byId[key].tournamentId == props.tournamentId ;
    }).map(key => allMatches.byId[key]);
});
this.testSub = this.store.pipe(select(fromRoot.getSpainMatches, {tournamentId: 68}))
      .subscribe(s => console.log('@@@@', s));

谢谢

【问题讨论】:

    标签: angular ngrx


    【解决方案1】:

    您可以尝试在某处使用 distinctUntilChanged……也许在您的订阅中。

    this.testSub = this.store.pipe(distinctUntilChanged(), select(fromRoot.getSpainMatches))
          .subscribe(s => console.log('@@@@', s));
    

    【讨论】:

      猜你喜欢
      • 2015-07-22
      • 2016-08-22
      • 2018-03-25
      • 2018-11-30
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多