- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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[]}>());
redcuer: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);
更新:
对 reducer 进行了 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);
数据现在像这样返回,但我需要摆脱外部 [] 我正在用 [ ] 包装数据响应,reducer 中的某个点,但这是我最接近它的功能正确:
[ [ { "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>
更新通过将推荐的更改添加到 reducer 中,我能够提出解决方案,但我不确定它是否是访问我需要的数据的正确方法
reducer
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);
最佳答案
你应该看看 example app由 ngrx 团队提供。
如果您尝试存储前端提供给您的数据,那么您的初始化是错误的
您应该有一个用于 session 室列表的界面
// have a model file:
export interface ConfRoom{
corpId: number;
site: string;
location: string;
floor: string;
}
// in your reducer
export interface State {
confList: ConfRoom[];
}
const initialState: State = {
confList: []
};
const _confRoomReducer = createReducer(
initialState,
// this line does nothing and can be delete...
// on(confRoomActionTypes.getRecentSearches, state=>
// state
// ),
// if recentSearches should change confList do this
on(confRoomActionTypes.getRecentSearchesloadSuccess,(state, {recentSearches} ) => ( {...state, confList: recentSearches})) )
// if recentSearches should be added to confList do this
on(confRoomActionTypes.getRecentSearchesloadSuccess,(state, {recentSearches} ) => ( {...state, confList: [...confList, recentSearches]})) )
如果您正在尝试获取后端数据:这是使用 api 调用和填充数据时的示例。
这是一个只有负载的简单 reducer :
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
import {
ProjectCollectionApiActions,
ProjectCollectionActions
} from '../../actions';
import { Project } from 'src/app/core/models';
export interface State extends EntityState<Project> {
loading: boolean;
loaded: boolean;
}
export const adapter: EntityAdapter<Project> = createEntityAdapter<Project>({
selectId: (project: Project) => project.id,
sortComparer: false,
});
export const initialState: State = adapter.getInitialState({
loading: false,
loaded: false
});
export const reducer = createReducer(
initialState,
on(ProjectCollectionActions.loadProjectCollection, (state) => ({
...state,
loading: true,
})),
on(ProjectCollectionApiActions.loadProjectsSuccess,
(state, { projects }) => adapter.addMany(projects, {
...state,
loading: false,
loaded: true
})
),
);
export const getLoaded = (state: State) => state.loaded;
export const getLoading = (state: State) => state.loading;
我将其导入名为 index.ts 的主文件
import {
createSelector,
createFeatureSelector,
combineReducers,
Action,
} from '@ngrx/store';
import * as fromDates from './reports/dates.reducer';
import * as fromSuperIntendents from './superintendents/superintendent.reducer';
import * as fromReports from './reports/reports.reducer';
import * as fromProjects from './projects/projects.reducer';
import * as fromMachines from './machines/machines.reducer';
import * as fromLaborers from './laborers/laborers.reducer';
import * as fromCollection from './reports/collection.reducer';
import * as fromRoot from '../../../state/reducers';
import { generateMockReport, Report } from 'src/app/reports/models';
export interface DataState {
dates: fromDates.State;
superintendents: fromSuperIntendents.State;
reports: fromReports.State;
laborers: fromLaborers.State;
collection: fromCollection.State;
projects: fromProjects.State;
machines: fromMachines.State;
}
export interface State extends fromRoot.State {
data: DataState;
}
export function reducers(state: DataState | undefined, action: Action) {
return combineReducers({
dates: fromDates.reducer,
superintendents: fromSuperIntendents.reducer,
reports: fromReports.reducer,
laborers: fromLaborers.reducer,
collection: fromCollection.reducer,
projects: fromProjects.reducer,
machines: fromMachines.reducer
})(state, action);
}
export const getDataState = createFeatureSelector<State, DataState>('data');
//here all my other reducers come
//...
//
export const getProjectsState = createSelector(
getDataState,
(state: DataState) => state.projects
);
export const {
selectIds: getProjectIds,
selectEntities: getProjectEntities,
selectAll: getAllProjects,
selectTotal: getTotalProjects,
} = fromProjects.adapter.getSelectors(getProjectsState);
export const getProjectsLoaded = createSelector(
getProjectsState,
fromProjects.getLoaded
);
export const getLoadedProjectIds = createSelector(
getProjectIds,
(ids) => { return ids as string[] }
)
export const getLoadedProjects = createSelector(
getProjectEntities,
getLoadedProjectIds,
(entities, ids) => {
return ids
.map(id => entities[id])
}
);
引用效果页面:
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import {
ProjectCollectionApiActions,
ProjectCollectionActions
} from '../../actions';
import { Project } from 'src/app/core/models';
import { LoggingService } from 'src/app/core/services/logging.service';
import { Update } from '@ngrx/entity';
import { ProjectService } from 'src/app/core/services/project.service';
@Injectable()
export class ProjectCollectionEffects {
loadProjectCollection$ = createEffect(() =>
this.actions$.pipe(
ofType(ProjectCollectionActions.loadProjectCollection),
switchMap(() => {
return this.projectService.getList().pipe(
map((projects: Project[]) =>
ProjectCollectionApiActions.loadProjectsSuccess({ projects })
),
catchError(error => {
this.logging.log('Error in loadCollection effect"',"Project load collection effect - within Approvals")
return of(ProjectCollectionApiActions.loadProjectsFailure({ error: {...error} }))
})
)
})
)
);
constructor(
private actions$: Actions,
private projectService: ProjectService,
private logging: LoggingService
) {}
}
编辑如果您没有 Id,您可以通过向服务中的数据添加一个来伪造一个:
let counter = 0;
return this.http.get<ConfRoom[]>(${url},httpOptions).pipe(
map((data: any) => {
let result: confRoom[] = [];
if(Array.isArray(data)){
data.forEach(room=> {
result.push({...room, id: counter});
counter = counter +1;
});
}
return result;
}),
catchError(handleError)
)
虽然听起来您只需要像我展示的第一部分那样的东西,但我提到您初始化错误的地方? (第一段代码)
关于angular - NGRX 8 reducer 返回对象而不是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320297/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!