JavaScript / Angular2 LocalStorage可以获得任何东西

时间:2021-05-27 23:00:04

I developing an Ionic2 app and am storing an array in local storage, it looks like the following:

我开发了一个Ionic2应用程序并将数组存储在本地存储中,它看起来如下所示:

Key | value options | [option1, option2, option3]

钥匙|价值选择| [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?

你知道为什么我得到一个空值吗?

1 个解决方案

#1


0  

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)

setItem不是LocalStorage的成员,使用它时必须出错。设置了正确的函数(键,值),在你的情况下localStorage.set('options',this.optionsArray)

docs with an example are here: http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/

带有示例的文档在这里:http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/

#1


0  

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)

setItem不是LocalStorage的成员,使用它时必须出错。设置了正确的函数(键,值),在你的情况下localStorage.set('options',this.optionsArray)

docs with an example are here: http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/

带有示例的文档在这里:http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/