首页 > 解决方案 > NgRx 取消订阅 store in effects

问题描述

我有一个简单的效果:

@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))效果,它观察到somethingFOREVER 的任何变化。

问题:

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

标签: angularngrx

解决方案


问题不在您显示的代码中,因为withLatestFrom只发出一个事件。安装Redux DevTools以查看您发送了哪些操作,并查看您是否没有发送两次“操作 A”


推荐阅读