Package Exports
- ngrx-store-localstorage-replace
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ngrx-store-localstorage-replace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ngrx-store-localstorage
Simple syncing between ngrx store and local storage
Dependencies
ngrx-store-localstorage depends on @ngrx/store and Angular 2.
Usage
npm install ngrx-store-localstorage --save- Import
composeandcombineReducersfrom@ngrx/storeand@ngrx/core/compose - Invoke the
localStorageSyncfunction aftercombineReducers, specifying the slices of state you would like to keep synced with local storage. - Optionally specify whether to rehydrate this state from local storage as
initialStateon application bootstrap. - Invoke composed function with application reducers as an argument to
StoreModule.provideStore.
import { Store, StoreModule } from '@ngrx/store';
import { todos, visibilityFilter } from './reducers';
import { NgModule } from '@angular/core'
@NgModule({
imports: [
BrowserModule,
StoreModule.provideStore(
compose(
localStorageSync(['todos']),
combineReducers
)({todos, visibilityFilter})
)
]
})
export class MyAppModule {}API
localStorageSync(keys : any[], rehydrateState : boolean = false, storage: Storage = localStorage) : Reducer
Provide state (reducer) keys to sync with local storage. Optionally specify whether to rehydrate initialState from local storage on bootstrap.
Returns a meta-reducer
Arguments
keysState keys to sync with local storage. The keys can be defined in two different formats:(string[]): array of strings representing the state (reducer) keys. Full state will be synced (e.g.
localStorageSync(['todos'])).(object[]): Array of objects where for each object the key represents the state key and the value represents custom serialize/deserialize options. This can be one of the following:
an array of properties which should be synced. This allows for the partial state sync (e.g.
localStorageSync([{todos: ['name', 'status'] }, ... ]))a reviver function as specified in the JSON.parse documentation
an object with properties that specify one or more of the following:
serialize: a function that takes a state object and returns a plain json object to pass to json.stringify
deserialize: a function that takes that takes the raw JSON from JSON.parse and builds a state object
replacer: a replacer function as specified in the JSON.stringify documentation
space: the space value to pass JSON.stringify
reviver: a reviver function as specified in the JSON.parse documentation
filter: an array of properties which should be synced (same format as the stand-along array specified above)
rehydrateState(boolean? = false): Pull initial state from local storage on startupstorage(Storage? = localStorage): Specify an object that conforms to the Storage interface to use, this will default localStorage