Created
November 25, 2019 18:04
-
-
Save wintercn/2e394d1cdb706d15e16e54398762a4ba 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
Vue.component('local-storage', { | |
props: ['value', 'key'], | |
template: `<div></div>`, | |
mounted(){ | |
if(!localStorage[this.$props.key]) | |
return; | |
var str = JSON.stringify(this.$props.value); | |
if(str != localStorage[this.$props.key]) | |
this.$emit('input', JSON.parse(localStorage[this.$props.key])) | |
}, | |
watch: { | |
value: { | |
deep: true, | |
handler(newVal, oldVal) { // watch it | |
console.log(newVal); | |
if(!this.updater) { | |
this.updater = () => { | |
console.log(this.$props.key); | |
localStorage[this.$props.key] = JSON.stringify(this.$props.value); | |
this.updater = null; | |
} | |
setTimeout(() => this.updater(), 1000); | |
} | |
} | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment