DevTools
// Reducer:constINITIAL_STATE=JSON.parse(localStorage.getItem('appState'));// app.component:
@HostListener('window:beforeunload',['$event'])unloadHandler(event: Event): void{
localStorage.setItem('appState',JSON.stringify(this.appState$.getValue()));}importmergefrom'lodash.merge';import{StoreModule,ActionReducer,MetaReducer}from'@ngrx/store';import{reducers}from'./reducers';exportfunctionstorageMetaReducer<S,AextendsAction=Action>(reducer: ActionReducer<S,A>){returnfunction(state: S,action: A): S{const nextState =reducer(state,action);constsavedState=JSON.parse(localStorage.getItem('appState'))||{};merge(nextState,savedState);localStorage.setItem('appState',nextState);returnnextState;};}exportconstmetaReducers: MetaReducer<any>[]=[storageMetaReducer];
@NgModule({imports: [StoreModule.forRoot(reducers,{ metaReducers })],})