vue-local-storage
vue-local-storage copied to clipboard
Storage of object as [object Object]
When storing object directly, it only store as [object Object].
Should we json encode before store?
Hello there.
You should provide ls variable type in localStorage section of Vue instance or call get/set methods with third parameter which allows to provide dynamic variable type:
Vue.localStorage.get('property', null, Object)
Vue.localStorage.set('property', { hey: 'there' }, Object)
我也遇到了该问题,去查看原型链,发现set方法没有第三个参数,下面是我的解决方式 this.$ls._lsSet('property', { hey: 'there' }, Object)
@shanghen you're trying to access private property. Yes, you can, but it's wrong from view of js naming agreements.
I'll implement third parameter ASAP. Be welcome to make a PR ;)
@pinguinjkeke
Is it implemented already? I've tried
Vue.localStorage.get('property', null, Object)
Vue.localStorage.set('property', { hey: 'there' }, Object)
And it doesn't work for me, unfortunately.
Of course, following code works for me, but it looks for me overcomplex and force me to define a preperty each time i want to use it.
Vue.localStorage.addProperty('user', Object);
Vue.localStorage.set('user', { name: 'John Doe' });
console.log(Vue.localStorage.get('user'));
Can't it (try to) detect type of value automatically? Also exists an issue of data merge
Vue.localStorage.set('user', { name: 'John Doe' });
Vue.localStorage.set('user', { id: 5 });
That code will overwrite "user" value instead of extend it
Do you have any clear plans for it?
Thank you!
@mikkiek I think the best way to implement it - forget about providing a type and use JSON.stringify/parse
for processing localStorage values.
I don't think that data merge is expected behavior.
@pinguinjkeke Yes, i've done it like you said. I'm using JSON.stringify/parse on localStorage.get and localStorage.set. And implemented "merge" for objects, for me it looks reasonable.
Thank you for your advices.
Use ._lsSet() instead of using .set()
@mikkiek I think the best way to implement it - forget about providing a type and use
JSON.stringify/parse
for processing localStorage values.I don't think that data merge is expected behavior.
What is the essence of the package if we have to do this?