javascript - NGRX 状态属性消失
问题描述
我正在尝试从数据库中获取用户信息。在组件中,我从服务中获取解码的 id,然后调用以 id 作为参数的操作。它返回用户,网络选项卡中有响应。状态属性“currentUser”一直为空,直到它变为响应,然后消失。
export interface State {
loading: boolean;
loggedIn: boolean;
currentUser: User;
}
const initialState: State = {
loading: false,
currentUser: null,
loggedIn: localStorage.getItem("token") ? true : false
};
case AuthActions.GET_USER_SUCCESS:
{
return {
...state,
loading: false,
loggedIn: true,
currentUser: action.user
};
}
@Effect()
getUserInfo$: Observable < Action > = this.actions$
.ofType(fromActions.GET_USER)
.pipe(map((action: fromActions.GetUser) => action.id),
concatMap(id => {
return this.authService.getUser(id);
})
)
.pipe(map((res: User) => ({
type: fromActions.GET_USER_SUCCESS,
payload: res
})));
}
解决方案
试试这样:
@Effect()
getUserInfo$: Observable<Action> = this.actions$
.ofType(fromActions.GET_USER)
.pipe(
map((action: fromActions.GetUser) => action.id),
concatMap(id =>
this.authService.getUser(id).pipe(
map((res: User) => ({
type: fromActions.GET_USER_SUCCESS,
payload: res
}))
)
)
);
推荐阅读
- c++ - 传递动态数组以在单独的函数中接收输入
- r - R Shiny:如何为标题面板的边距着色?
- java - 如何使用 Room 创建新的数据库实例?
- javascript - Javascript:使用条带之间的分隔符构建css线性渐变
- php - 表格不考虑宽度 OpenOffice / MsWord
- powershell - 将带有对象布局的字符串转换为对象
- vb.net - 自定义最小化按钮丢失原始最小化按钮动画
- python - Kivy:[警告] [AudioSDL2] 无法加载 beep-01a.wav:b'Mix_LoadWAV_RW with NULL src'
- ios - iOS13 上的 UISearchController 崩溃
- unity3d - 如何在不重新加载整个视频的情况下更改分辨率?