重置 - Redux 抛出分派的无效操作:未定义

Reset - Redux throws dispatched an invalid action: undefined

我正在尝试根据注销重置我的 redux-store,但出现以下错误。寻找更好的建议来重置我的 redux-store 状态,以便我发现将其设置为未定义的任何地方都会有所帮助,但在我的情况下它不起作用。

    /**
 * Our state is composed of a map of action reducer functions.
 * Here we are going to reset the state of root reducer to undefined to clear data store
 * Logged Action is triggered from User session effects.
 */
export function resetState(reducer: ActionReducer<State>): ActionReducer<State> {
  return function (state: State, action: UserSessionActions.UserSessionActionsUnion): State {

    switch (action.type) {
      case UserSessionActions.UserSessionActionTypes.LoggedOut: {
        state = undefined;
        return reducer(state, action);
      }
      default: {
        return reducer(state, action);
      }
    }
  };
}

但是得到这个异常

ERROR Error: Effect "UserSessionEffects.logout$" dispatched an invalid action: [object Object]
at reportInvalidActions (effects.es5.js:236)
at verifyOutput (effects.es5.js:214)
at MapSubscriber.project (effects.es5.js:284)
at MapSubscriber._next (map.js:79)
at MapSubscriber.Subscriber.next (Subscriber.js:90)
at SwitchFirstMapSubscriber.notifyNext (exhaustMap.js:113)
at InnerSubscriber._next (InnerSubscriber.js:23)
at InnerSubscriber.Subscriber.next (Subscriber.js:90)
at MapSubscriber._next (map.js:85)
at MapSubscriber.Subscriber.next (Subscriber.js:90)

这是我的效果

    @Effect()
  logout$ = this.actions$.pipe(
    ofType(UserSessionActions.UserSessionActionTypes.LoggedOut),
    map((action: any) => {
      this.logger.log('logout ' + JSON.stringify(action));
      return Observable.of({});
    }),
    catchError((err) => {
      this.logger.log(err);
      return Observable.of();
    })
  );

ERROR Error: Effect "UserSessionEffects.logout$" dispatched an invalid action

A ngrx/effect 必须 return 一个动作(类型为 属性 的对象),除非它被装饰为 @Effect({dispatch: false}).

将您的效果更新为 return 一个动作:

return Observable.of({ type: 'LOGGED OUT SUCCESS'});