[ngrx / store状态未在称为reducer的函数中更新

问题描述 投票:1回答:1

我开始将ngrx(8.4.0)添加到现有的Angular(8.2)应用程序中,但是被触发时我的一个操作(请参阅下面的操作)不会使状态发生变化。

auth.actions.ts(部分)

export const loginSuccess = createAction(
  '[SIGN IN] Login Success',
  props<{ user: any; isNewUser: boolean }>()

loginSuccess操作由下面的reducer函数处理。

auth.reducer.ts(部分)

export interface AuthState {
  user: any;
  isNewUser: boolean;
}

export const initialState: AuthState = {
  user: null,
  isNewUser: null
};

const authReducer = createReducer(
  initialState,
  on(loginSuccess, (state, { user, isNewUser }) => ({
    ...state,
    user: user,
    isNewUser: isNewUser
  }))
);

export function reducer(state: AuthState | undefined, action: Action) {
  return authReducer(state, action);
}

loginSuccess操作是从名为loginWithPopUp的效果中分派的。

  loginWithPopUp$ = createEffect(() =>
    this.actions$.pipe(
      ofType(authActions.loginWithPopUp),
      distinctUntilChanged(),
      switchMap(action =>
        from(this.authService.signUpWithPopUp()).pipe(
          map((result: firebase.auth.UserCredential) =>
            authActions.loginSuccess({
              user: result.user,
              isNewUser: result.additionalUserInfo.isNewUser
            })
          ),
          tap(() => this.router.navigate(['/notes'])),
          catchError(() => of(authActions.loginFail()))
        )
      )
    )
  );

即使触发了我的动作,并且在动作中看到属性userisNewUser,状态也不会更新。

enter image description here

angular rxjs ngrx ngrx-store ngrx-effects
1个回答
0
投票

代码看起来不错,您可以验证一下:

  • reducer被调用,您可以在其中放置console.log或调试语句
  • 确保在操作中填充了用户和isNewUser,您可以通过在devtools中单击操作切换来执行此操作
© www.soinside.com 2019 - 2024. All rights reserved.