NgRx取消订阅效果商店

时间:2020-11-09 22:41:30

标签: angular ngrx

我有一个简单的效果:

@Effect()
simpleEffect = this.actions.pipe(
  ofType<ActionA>('Action A'),
  withLatestFrom(this.store.select(selectSomething)),
  map(([action, something]) => console.log('CALLED TWICE'))
);

这里是选择器:

export const selectSomething = createSelector(
  (appState) => appState.someState,
  (someState) => someState.something
)

以下是一个简单的reducer,它更新something的值:


export const someReducer = (state: SomeState = someInitialState, action: ActionB) => {
  switch (action.type) {
    case 'Action B':
      return {
        ...state,
        something: action.payload
      };
    default:
      return state;
  }
};

问题:

当我分发ActionB时,尽管没有调用ActionA的效果。

正如我所发现的,它的发生是由于效果withLatestFrom(this.store.select(selectSomething))永远观察到something的任何变化。

问题:

是否可以退订该选择器?还是有其他功能代替withLatestFrom

2 个答案:

答案 0 :(得分:0)

问题不在您显示的代码中,因为withLatestFrom仅发出一个事件。 安装Redux DevTools来查看要发送的操作,以及是否不两次发送“操作A”

答案 1 :(得分:0)

问题相似但不相同。

老实说,我对ActionC产生了另一种影响:

        StringBuilder sb = new StringBuilder();
        String data = newFeedModel.getFeedTags();
        String[] items = data.split("#");
        for (String item : items)
        {
            if(!item.isEmpty()){
                sb.append("#"+item+"\n");
            }
        }
        holder.tags.setText(sb.toString());

更改@Effect otherEffect = this.actions.pipe( ofType<ActionC>('Action C'), switchMap(() => this.store.select(selectSomethingElse)), switchMap(() => of(new ActionA())) ) 时会调用它。

我没有故意派遣ActionA,但是somethingElsesimpleEffect上订阅的otherEffect触发了