Last active
November 10, 2018 08:52
-
-
Save tatat/01476104dae9afc49c809b3e8904191f 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 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