【发布时间】:2019-12-18 18:32:03
【问题描述】:
reducer 返回的数据是一个对象,但我需要它是一个数组。 我已经尝试返回 action.recentSearches 但它似乎不起作用。
正在返回的数据是:
{ "loading": false, "recentSearches": [ { "corpId": "123", "site": "location", "building": "location", "floor": "2N" }, { "corpId": "123", "site": "location", "building": "location", "floor": "09" }, { "corpId": "123", "site": "location", "building": "location", "floor": "01" } ] }`
行动:
export const getRecentSearches = createAction(
'[ConfRoom API] Request Recent Searches'
);
export const getRecentSearchesloadSuccess = createAction('[ConfRoom API] Recent Searches Load Success', props<{recentSearches: RecentSearchesModel[]}>());
还原器: console.log 确实打印了我需要的值,但返回 action.recentSearches 不起作用
export const initialState = [];
const _confRoomReducer = createReducer(
initialState,
on(confRoomActionTypes.getRecentSearches, state=> ({
...state
})),
on(confRoomActionTypes.getRecentSearchesloadSuccess,(state, { recentSearches }) => ({ ...state, recentSearches })) )
export function confRoomReducer(state, action) {
console.log(action.recentSearches);
return _confRoomReducer(state, action);
组件中的值
recentSearches$: Observable<RecentSearchesModel[]> = this.store.select(state => state.recentSearches);
更新:
对减速器进行了 2 次编辑:
export const state = [];
export const initialState = [];
const _confRoomReducer = createReducer(
initialState,
on(confRoomActionTypes.getRecentSearches, state=>
state
),
on(confRoomActionTypes.getRecentSearchesloadSuccess,(state, {recentSearches} ) => ([ ...state, recentSearches ])) )
export function confRoomReducer(state, action) {
return _confRoomReducer(state, action);
数据现在正在像这样返回,但我需要摆脱外部 [] 我用 [] 包装数据响应,在减速器中的某个点,但这是我最接近它的功能正确:
[ [ { "corpId": "123", "site": "location", "building": "location, "floor": "01,02" }, { "corpId": "123", "site": "location", "building": "location", "floor": "01,02" } ] ]
我的 ngFor 可以读取数据,但它没有按预期工作,因为它需要我添加要显示的数据的索引:
<tr *ngFor="let recentSearch of recentSearches$ | async; let i = index" ng-class-odd="'striped'">
<td>{{recentSearch[0]}}, {{recentSearch[0].building}}, {{recentSearch[0].floor}}</td>
</tr>
更新 通过向减速器添加推荐的更改,我能够提出解决方案,但我不确定这是访问我需要的数据的正确方法
减速器
export interface RecentSearchesModel {
site: string;
corpId: string;
building: string;
floor: string
}
export interface State {
resultList: RecentSearchesModel[];
}
const initialState: State = {
resultList: []
};
const _confRoomReducer = createReducer(
initialState,
on(confRoomActionTypes.getRecentSearches, state => ({
...state
})),
on(
confRoomActionTypes.getRecentSearchesloadSuccess,
(state, { recentSearches }) => ({ ...state, resultList: recentSearches })
)
);
export function confRoomReducer(state, action) {
return _confRoomReducer(state, action);
}
数据 现在数据是这样返回的
{ "resultList": [ { "corpId": "123", "site": "CHINA", "building": "BUILDING 12", "floor": "2N" }, { "corpId": "123", "site": "US", "building": "BIG BUILDING", "floor": "09" }, { "corpId": "123", "site": "LONDON", "building": "BIG BEN", "floor": "01" } ] }
但要在组件中访问我想要的数据,我必须编辑模型并添加 resultList[]
export interface RecentSearchesModel {
corpId: string;
site: string;
building: string;
floor: string;
resultList[];
}
我觉得我不应该将 resultList 添加到我的模型中,因为数据从未真正映射到它,我只是使用它来访问与数据关联的标签 组件
recentSearches$: Observable<RecentSearchesModel[]> = this.store.select(state => state.recentSearches.resultList);
【问题讨论】:
-
尝试 [...state] 而不是 {...state}
-
您在使用实体适配器吗?请在上面的示例中包含您的 State 接口和 initialState
-
嗨,我已经更新以显示完整的减速器文件,我是 ngrx 的新手,所以我可能会错误地实施这些
-
嗨,我已经做出了建议的更改,并为我的问题提出了解决方案,我已经对我的问题进行了更新以反映这一点,非常感谢你对此的看法@JamesD
-
正如您将在我提供的示例应用程序链接中看到的那样,您将在底部看到如何制作选择器,您正在导出一个函数,但您应该通过选择器传递数据。我建议您克隆该示例应用程序并运行它。如果您遵循它,您会发现如何使用最新版本正确实现 ngrx。所有选择器都构建在一个主减速器文件 ref 中。 github.com/ngrx/platform/blob/master/projects/example-app/src/… 是这些选择器,您将专门调用这些选择器来获取所需的状态片段,在您的情况下为 resultList
标签: angular typescript ngrx angular8 state-management