JavaScript / Angular2 LocalStorage可以获得任何东西 [英] JavaScript/Angular2 LocalStorage can get anything
问题描述
我正在开发一个Ionic2应用并将数组存储在本地存储中,它看起来如下所示:
I developing an Ionic2 app and am storing an array in local storage, it looks like the following:
Key |价值
期权| [option1,option2,option3]
Key | value options | [option1, option2, option3]
我正在尝试使用以下方式获取期权
密钥:
I am trying to get options
key using:
constructor(nav) {
this.nav = nav;
this.local = new Storage(LocalStorage);
this.optionsArray = this.local.get('options');
console.log('content of array : '+JSON.stringify(this.optionsArray));
}
我在控制台中得到的结果:
What I get in the console:
数组内容:{}
content of array : {}
以下是我如何设置数组的方法上一页:
Here is how I set the array in previous pages:
this.optionsArray = ['option1','option2','option3'];
localStorage.setItem('options', this.optionsArray);
你知道为什么我得到一个空值吗?
Do you know why I am getting an empty value?
推荐答案
setItem
不是 LocalStorage
的成员,你使用它时必须出错。正确的函数是 set(key,value)
,在你的情况下 localStorage.set('options',this.optionsArray)
setItem
is not a member of LocalStorage
, you must be getting an error when you use it. The correct function is set(key,value)
, in your case localStorage.set('options',this.optionsArray)
带有示例的文档在这里: http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/
docs with an example are here: http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/
这篇关于JavaScript / Angular2 LocalStorage可以获得任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!