我开始将 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
,状态也没有更新。
user
和 isNewUser
填充在操作中。
控制台上出现的错误。
最佳答案
代码看起来不错,你能验证一下吗:
- reducer 被调用,你可以在里面放一个 console.log 或者调试语句
- 确保用户和 isNewUser 填充在操作上,您可以通过单击开发工具中的操作开关来执行此操作
关于angular - ngrx/store 状态未在调用的 reducer 函数中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58532464/