Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // actions.ts
- const STORAGE = '@ngrx/store/storage';
- export class Storage implements Action {
- readonly type = STORAGE;
- constructor(readonly payload: string) {}
- }
- // reducers.ts
- import { localStorageSync, rehydrateApplicationState } from 'ngrx-store-localstorage';
- export function localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
- return (state: State, action: All) => {
- const keys = ['key1', 'key2'];
- if (action.type === STORAGE && keys.includes(action.payload)) {
- const rehydratedState = rehydrateApplicationState([action.payload], localStorage, k => k);
- return { ...state, ...rehydratedState };
- }
- return localStorageSync({
- keys,
- rehydrate: true,
- })(reducer)(state, action);
- }
- }
- // app.component.ts
- export class AppComponent implements OnInit {
- constructor(
- private readonly renderer: Renderer2,
- private readonly store: Store<State>,
- ) {}
- ngOnInit() {
- this.renderer.listen('window', 'storage', event => {
- this.store.dispatch(new Storage(event.key));
- });
- }
- }
Add Comment
Please, Sign In to add comment