Last active
November 10, 2018 08:16
-
-
Save tatat/f97e25811aff39acf4a536bb3c09d403 to your computer and use it in GitHub Desktop.
動作未確認
This file contains hidden or 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
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