Skip to content

Instantly share code, notes, and snippets.

@wintercn
Created November 25, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wintercn/2e394d1cdb706d15e16e54398762a4ba to your computer and use it in GitHub Desktop.
Save wintercn/2e394d1cdb706d15e16e54398762a4ba to your computer and use it in GitHub Desktop.
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