Angular ngrx-在对象内部有数组的更新存储

时间:2020-10-26 20:56:30

标签: angular ngrx

我有这样的效果:

loadFolder$ = createEffect(() => {
return this.action$.pipe(
  ofType(SidebarAction.loadFirmSuccess),
  switchMap(action => {
    const foldersList = {};

    if(!action.selectedFirm) {
      action.firms[0].roles.map((role: string) => {
        foldersList[role.toLowerCase()] = this.sidebarService.getFolders(action.userId, action.firms[0].id.toString() , role, action.page.toString(), action.size.toString());
      });
    }

    else {
      action.selectedFirm.roles.map((role: string) => {
        // tslint:disable-next-line: no-non-null-assertion
        foldersList[role.toLowerCase()] = this.sidebarService.getFolders(action.userId, action.selectedFirm!.id.toString() , role, action.page.toString(), action.size.toString());
      });
    }

    return forkJoin(foldersList).pipe(map(res => {
      return SidebarAction.loadFolderSuccess({folders : res, selectedFirm: action.selectedFirm});
    }));
  })
);
});

它返回我这样的文件夹对象

 folders: {
    admin: [[{idFolder: 1, folderName: 'folder1'}], {page: 1, size: 2}, {amount: 3}],
    superadmin: [[{idFolder: 1, folderName: 'folder1'}], {page: 1, size: 2}, {amount: 3}],
    user: [[{idFolder: 1, folderName: 'folder1'}], {page: 1, size: 2}, {amount: 3}]
 }

所有键都不存在...要视情况而定。

这是问题,我的第一个结果是这样的:

 folders: {
    admin: [[{idFolder: 1, folderName: 'folder1'}, {idFolder: 2, folderName: 'folder2'}], {page: 1, size: 2}, {amount: 4}]
 }
 

当我增加页面时,我会为获得其他结果而调度相同的动作

  folders: {
    admin: [[{idFolder: 3, folderName: 'folder3'}, {idFolder: 4, folderName: 'folder4'}]{page: 2, size: 2}{amount: 4}]
 }

这是减速器:

  export const sideBarReducer = createReducer(
   initialSidebarState,
   on(SidebarActions.loadFolderSuccess, (state, action): SidebarState => {
      return {
               ...state,
               folders: action.folders,
               selectedFirm : action.selectedFirm
      };
    }),
  );

action.folders可能是这样的对象

 folders: {
    admin: [[{idFolder: 1, folderName: 'folder1'}],{page: 1, size: 2},{amount: 3}],
    superadmin: [[{idFolder: 1, folderName: 'folder1'}],{page: 1, size: 2},{amount: 3}],
    user: [[{idFolder: 1, folderName: 'folder1'}],{page: 1, size: 2},{amount: 3}]
 }

我想用新状态更新状态以获得以前的状态,

  folders: {
    admin: [[{idFolder: 1, folderName: 'folder1'}, {idFolder: 2, folderName: 'folder2'}, {idFolder: 3, folderName: 'folder3'}, {idFolder: 4, folderName: 'folder4'}],{page: 2, size: 2},{amount: 3}],
    superadmin: [[{idFolder: 1, folderName: 'folder1'}],{page: 1, size: 2},{amount: 3}],
    user: [[{idFolder: 1, folderName: 'folder1'}],{page: 1, size: 2},{amount: 3}]
 }

但是使用reducer我会看到这种状态:

     folders: {
       admin: [[{idFolder: 3, folderName: 'folder3'}, {idFolder: 4, folderName: 'folder4'}],{page: 2, size: 2},{amount: 3}],
  
     }

我试图在化简器中使用扩展运算符,但是它不起作用。显而易见,因为更改位于文件夹对象的数组内部。

  export const sideBarReducer = createReducer(
   initialSidebarState,
   on(SidebarActions.loadFolderSuccess, (state, action): SidebarState => {
      return {
               ...state,
               folders: {...state.folders, action.folders},
               selectedFirm : action.selectedFirm
      };
    }),
  );

1 个答案:

答案 0 :(得分:0)

对象传播仅进行浅层合并。 您需要进行深度合并,例如可以使用merge