Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active November 10, 2018 08:52
Show Gist options
  • Save tatat/01476104dae9afc49c809b3e8904191f to your computer and use it in GitHub Desktop.
Save tatat/01476104dae9afc49c809b3e8904191f to your computer and use it in GitHub Desktop.
動作未確認
const persistedStore = {
state: {},
load() {
this.state = JSON.parse(localStorage.getItem('state'))
},
watch(vm, key) {
return vm.$watch(key, (value) => {
localStorage.setItem('state', JSON.stringify(value))
}, { deep: true })
}
}
const memoryStore = {
state: {}
}
persistedStore.load()
Vue.mixin({
data() {
return {
persisted: persistedStore.state,
memory: memoryStore.state
}
}
})
// App.vue とかで
export default {
mounted() {
this._unwatch = persistedStore.watch(this, 'persisted')
},
beforeDestroy() {
this._unwatch()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment