Created
January 18, 2017 13:20
-
-
Save toast38coza/6b0679b3a049069fbc4e91a63a2eb1d8 to your computer and use it in GitHub Desktop.
Dump the entire vuex store to localStorage and retrieve it again.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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