简体   繁体   English

本地存储未在 Angular 中保存 NGRX state

[英]Local storage is not saving the NGRX state in Angular

I am using NGRX for state management in Angular.我在 Angular 中使用 NGRX 进行 state 管理。 NGRX is working fine. NGRX 工作正常。 I am also using local storage to save the NGRX state but it is not working, when I do refresh in the browser then ngrx data reset to initial state.我也在使用本地存储来保存 NGRX state 但它不起作用,当我在浏览器中刷新时,ngrx 数据重置为初始 state。 And when I use developer debugging tool then after going to Redux section I verified it that ngrx is working fine.当我使用开发人员调试工具时,在转到 Redux 部分后,我验证了 ngrx 工作正常。 But when I go the developer debugging tool Application section and when I see the app state in its initial state and after deleting the state from there and doing refresh, it does not show app state. But when I go the developer debugging tool Application section and when I see the app state in its initial state and after deleting the state from there and doing refresh, it does not show app state.

app.reducers.ts应用程序.reducers.ts

 import { ActionReducerMap, combineReducers } from '@ngrx/store'; import { PointReducers } from '../point/shared/store/point.reducers'; import { AppState } from './app.state'; export const appReducers: ActionReducerMap<AppState> = { cashPoint: combineReducers({ closingTab: PointReducers.closingTab, configTab: PointReducers.configTab, departTab: PointReducers.departTab, }) };

store.reducers.ts store.reducers.ts

 import { Action, ActionReducer } from '@ngrx/store'; import { LocalStorageConstants } from '../shared/constants/local-storage.constants'; import { AppState } from './app.state'; export function storeMetaReducers(reducer: ActionReducer<any>) { return function (state: AppState | undefined, action: Action) { if (state === undefined) { const persisted = localStorage.getItem(LocalStorageConstants.AppState); return persisted? JSON.parse(persisted): reducer(state, action); } const newState = reducer(state, action); localStorage.setItem(LocalStorageConstants.AppState, JSON.stringify(newState)); return newState; }; }

app.module.ts app.module.ts

 import { APP_BASE_HREF } from '@angular/common'; import { HttpClient, HttpClientModule } from '@angular/common/http'; import { CUSTOM_ELEMENTS_SCHEMA, NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { EffectsModule } from '@ngrx/effects'; import { StoreModule } from '@ngrx/store'; import { StoreDevtoolsModule } from '@ngrx/store-devtools'; import { TranslateCompiler, TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { TranslateMessageFormatCompiler } from 'ngx-translate-messageformat-compiler'; import { PointModule } from '../../../app/src/lib/components/app/point/point.module'; import { createTranslateLoader } from '../../../app/lib/components/app/point/shared/helpers/helpers'; import { appReducers } from '../../../app/lib/components/app/store/app.reducers'; import { AppComponent } from './app.component'; import { AppRoutingModule } from './app.routing'; @NgModule({ declarations: [AppComponent], imports: [ BrowserModule, BrowserAnimationsModule, FormsModule, HttpClientModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: createTranslateLoader, deps: [HttpClient] }, compiler: { provide: TranslateCompiler, useClass: TranslateMessageFormatCompiler } }), AppRoutingModule, CashPointModule, StoreModule.forRoot(appReducers, { runtimeChecks: { strictStateImmutability: false, strictActionImmutability: false, strictStateSerializability: false, strictActionSerializability: false } }), EffectsModule.forRoot([]), StoreDevtoolsModule.instrument({ maxAge: 50 }), ], providers: [ { provide: APP_BASE_HREF, useValue: '/' } ], bootstrap: [AppComponent], schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA] }) export class AppModule {}

It can work by doing the following changes:它可以通过执行以下更改来工作:

MetaReducer stuff is missing in app.reducers.ts . app.reducers.ts中缺少MetaReducer的东西。 Add these lines in app.reducers.tsapp.reducers.ts中添加这些行

 import { ActionReducerMap, combineReducers, MetaReducer } from '@ngrx/store'; export const metaReducers: MetaReducer<AppState>[] = [storeMetaReducers];

metaReducers stuff is missing in app.module.ts . app.module.ts中缺少metaReducers的东西。 Add these lines in app.module.tsapp.module.ts中添加这些行

 import { appReducers, metaReducers } from '../../../cashbook-lib/src/lib/components/app/store/app.reducers'; //Add metaReducers in StoreModule.forRoot StoreModule.forRoot(appReducers, { metaReducers, }),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM