问题描述
我收到一个错误,我无法理解原因。
我已经阅读了有关效果创建的所有内容,但仍然无法确定此错误原因。
我是 angular 的新手,尤其是 Ngrx,所以也许我没有看到明显的东西。
我创建了一个操作:
export interface ServerAuthData{
AccessToken: string;
RefreshToken: string;
Utente: Utente;
}
export interface AuthState {
isAuthenticated: boolean;
errorMessage: string;
accessToken: string;
refreshToken: string;
utente: Utente;
}
export const authLogin = createAction(
'[Auth] Login',props<{ username: string,password: string }>()
);
它的减速器:
const reducer = createReducer(
initialState,on(authLogin,(state) => ({ ...state })),on(authLogout,(state) => (
{ ...state,isAuthenticated: false,utente: null,accessToken: "",refreshToken: "",})),on(authLoginSuccess,(state,{authState}) => (
{ ...state,isAuthenticated: true,utente: authState.utente,accessToken: authState.accessToken,refreshToken: authState.refreshToken,erroMessage: authState.errorMessage,}))
);
我要调用的服务来登录用户:
login(username: string,password: string) {
var formData: any = new FormData();
formData.append("username",username);
formData.append("password",password);
console.log("Chiamo il server");
return this.http.post<ServerAuthData>(this.loginUrl,formData);
}
最后是效果:
login$ =
createEffect(() =>
this.actions$.pipe(
ofType(authLogin),switchMap((action) =>
this.authService.login(action.username,action.password).pipe(
tap((serverAuthData) => console.log('Dopo login. Ricevuto:' + serverAuthData)),map((serverAuthData) =>
console.log("Converting data");
authLoginSuccess({authState:{
isAuthenticated: true,accessToken: serverAuthData.AccessToken,errorMessage: "",refreshToken : serverAuthData.RefreshToken,utente: serverAuthData.Utente,}}
)),catchError((error: any) => of(this.authService.loginFailure(error)))
)
)
)
);
在使用 map 而不是 switchMap 和 {dispatch: false} 之前一切正常,但当然没有新的 action 被调度。 切换到 switchMap 并删除 {dispatch: false} 产生了错误:
输入'可观察的
输入'可观察的
输入 'void | ({ authState: AuthState; } & TypedAction)' 不可分配给类型 'Action'。
类型 'void' 不能分配给类型 'Action'。
有人可以帮我吗?
编辑:我知道 switchMap 必须返回一个值。分派一个新动作不返回一个值吗?我也试过 return authLoginSuccess 但没办法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)