Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage of object as [object Object] #36

Open
libern opened this issue Jan 16, 2018 · 8 comments
Open

Storage of object as [object Object] #36

libern opened this issue Jan 16, 2018 · 8 comments

Comments

@libern
Copy link

libern commented Jan 16, 2018

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

Should we json encode before store?

@pinguinjkeke
Copy link
Owner

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 pinguinjkeke self-assigned this Jan 16, 2018
@shanghen
Copy link

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

@pinguinjkeke
Copy link
Owner

@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 ;)

@mikkiek
Copy link

mikkiek commented Jan 28, 2018

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

@pinguinjkeke
Copy link
Owner

pinguinjkeke commented Jan 30, 2018

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

@mikkiek
Copy link

mikkiek commented Jan 30, 2018

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

@580
Copy link

580 commented Jun 12, 2018

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

@Ritcheyy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants