How to Store Array Data Using Ionic Native Storage?

蹲街弑〆低调 提交于 2019-12-10 14:50:51

问题


I'm planning to use ionic native storage to store some translation history, whenever there's a word being translated. The translation action (date, translate word) will be store in the ionic native storage, and when I open history page, a list of translation history will be shown.

Here's the most basic code I got from the ionic official website:

export class HomePage {
  DataArray: Array<string> = [];

  constructor(public navCtrl: NavController, private storage: Storage) {

  }
  // set a key/value
  setData(){
  this.storage.set('age', 'Max');
  }
  // Or to get a key/value pair
  getData(){
  this.storage.get('age').then((val) => {
    console.log('Your age is', val);
  });
}
}

回答1:


use getItem and SetItem

export class HomePage {
  DataArray: Array<string> = [];

  constructor(public navCtrl: NavController, private storage: NativeStorage) {

  }
  // set a key/value
  setData(){
  this.storage.setItem('keyOfData', JSON.stringify(DataArray));
  }
  // Or to get a key/value pair
  getData(){
  this.storage.getItem('keyOfData').then((val) => {
    console.log('Your age is', JSON.parse(val));
  });
}
}

the refrence Ionic native storage



来源:https://stackoverflow.com/questions/44473329/how-to-store-array-data-using-ionic-native-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!