Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active November 10, 2018 08:16
Show Gist options
  • Save tatat/f97e25811aff39acf4a536bb3c09d403 to your computer and use it in GitHub Desktop.
Save tatat/f97e25811aff39acf4a536bb3c09d403 to your computer and use it in GitHub Desktop.
動作未確認
const store = {
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 })
}
}
store.load()
Vue.mixin({
data() {
return { sharedState: store.state }
}
})
// App.vue とかで
export default {
mounted() {
this._unwatch = store.watch(this, 'sharedState')
},
beforeDestroy() {
this._unwatch()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment