Skip to content

Instantly share code, notes, and snippets.

@toast38coza
Created January 18, 2017 13:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toast38coza/6b0679b3a049069fbc4e91a63a2eb1d8 to your computer and use it in GitHub Desktop.
Save toast38coza/6b0679b3a049069fbc4e91a63a2eb1d8 to your computer and use it in GitHub Desktop.
Dump the entire vuex store to localStorage and retrieve it again.
// key to use for get/set state:
export const LOCALSTORAGE_KEY = '..'
// as an action:
export const PERSIST_ACTION = ({ rootState }) => {
let stateAsString = JSON.stringify(rootState)
window.localStorage.setItem(LOCALSTORAGE_KEY, stateAsString)
}
// restore a cached vuex store:
// in any vuex item:
loadState () {
let stateCache = window.localStorage.getItem(LOCALSTORAGE_KEY)
store.replaceState(JSON.parse(stateCache))
}
// profit :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment