我开始将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()))
)
)
)
);
即使触发了我的动作,并且在动作中看到属性user
和isNewUser
,状态也不会更新。
代码看起来不错,您可以验证一下: