vue-local-storage icon indicating copy to clipboard operation
vue-local-storage copied to clipboard

Storage of object as [object Object]

Open libern opened this issue 6 years ago • 8 comments

When storing object directly, it only store as [object Object].

Should we json encode before store?

libern avatar Jan 16 '18 11:01 libern

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)

pinguinjkeke avatar Jan 16 '18 11:01 pinguinjkeke

我也遇到了该问题,去查看原型链,发现set方法没有第三个参数,下面是我的解决方式 this.$ls._lsSet('property', { hey: 'there' }, Object)

shanghen avatar Jan 19 '18 08:01 shanghen

@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 avatar Jan 19 '18 08:01 pinguinjkeke

@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 avatar Jan 28 '18 21:01 mikkiek

@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 avatar Jan 30 '18 16:01 pinguinjkeke

@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.

mikkiek avatar Jan 30 '18 18:01 mikkiek

Use ._lsSet() instead of using .set()

580 avatar Jun 12 '18 16:06 580

@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?

Ritcheyy avatar Nov 16 '20 07:11 Ritcheyy