【问题标题】:Angular/Ionic Storage, how to store data dynamicallyAngular/Ionic Storage,如何动态存储数据
【发布时间】:2020-02-06 13:52:57
【问题描述】:

所以我有一些代码允许用户选择联系人并将数据解析为数组。 但是,问题是无法永久存储该数据,因为用户离开组件时,数据就会消失。

我一直在尝试使用 Ionic 存储来存储该数据,但我不知道如何正确执行此操作。

这里是相关代码。

export class ContactComponentComponent implements OnInit {

  constructor(private contacts: Contacts, private storage: Storage) {
   this.getContact();
  }

  mContacts = [
   {
    id: [0],
    name: '',
    number: ''
   },
   {
    id: [1],
    name : [''],
    number: ['']
   },
   {
    id: [2],
    name : [''],
    number: ['']
   },
   {
    id: [3],
    name : [''],
    number: ['']
   },
   {
    id: [4],
    name : [''],
    number: ['']
   }
  ];

  ngOnInit() {}

  pickContact(contactId) {
    this.contacts.pickContact().then((contact) => {
      this.mContacts[contactId].name = contact.name.givenName;
      this.mContacts[contactId].number = contact.phoneNumbers[0].value;
      this.storage.set('contact-name', JSON.stringify(this.mContacts.name));
      this.storage.set('contact-number',  JSON.stringify(this.mContacts.number));
    });
  }

  getContact()
  {
    this.storage.get('contact-name').then((val) => {
      console.log('Contact Name: ', val);
    });
    this.storage.get('contact-number').then((val) => {
      console.log('Contact Number:', val);
    });
  }

}

这里是 HTML

<ion-list>

  <ion-grid>
    <ion-row>
      <ion-col>
        <ion-item-group   *ngFor="let contacts of mContacts">
          <ion-card (click) = "pickContact(contacts.id)">
              <ion-item lines = "none">             
                  <ion-label class="ion-text-wrap">Name: {{contacts.name}}</ion-label>        
                </ion-item>
                <ion-item lines = "none" >
                  <ion-label class="ion-text-wrap">Number: {{contacts.number}}</ion-label>
                </ion-item>       
          </ion-card>            
        </ion-item-group>    

      </ion-col>
    </ion-row>

  </ion-grid>
</ion-list>

我知道这是很糟糕的代码,但我不知道如何在用户选择并检索该数组后在调用组件时保存它

【问题讨论】:

  • 你想实现这个:将最后的数据存储到后端,当用户回来时,从后端检索数据并放入存储中?您使用的是哪个后端?
  • 我正在使用 Ionic 存储作为数据库,这就是您要问的吗?
  • 我应该使用 foreach 函数吗?

标签: angular typescript ionic4 ionic-storage


【解决方案1】:

我终于找到了答案, 希望这对某人有所帮助。


  constructor(private contacts: Contacts, private storage: Storage) {
   this.getContact();
  }
  key:string = "contacts";
  mContacts = [
   {
    id: [0],
    name: '',
    number: ''
   },
   {
    id: [1],
    name : [''],
    number: ['']
   },
   {
    id: [2],
    name : [''],
    number: ['']
   },
   {
    id: [3],
    name : [''],
    number: ['']
   },
   {
    id: [4],
    name : [''],
    number: ['']
   }
  ];

  ngOnInit() {}

  pickContact(contactId) {

    this.contacts.pickContact().then((contact) => {
      this.mContacts[contactId].name = contact.name.givenName;
      this.mContacts[contactId].number = contact.phoneNumbers[0].value;
      this.saveContacts();
    });
  }
  saveContacts(){
    this.storage.set(this.key, JSON.stringify(this.mContacts));
  }
  getContact()
  {
    this.storage.get(this.key).then((val) => {
      console.log('Contact Name: ', val);
      if (val != null && val !== undefined) {
        this.mContacts = JSON.parse(val);
      }
    });

  }


}

【讨论】:

  • 存储部分是对的。不过,您对联系人的表示法似乎很奇怪。像name: [''], number: [''] 这样的操作是将值放在一个数组中,其中包含一个空字符串。您不需要 [ ] 部分仅用于一个值。
猜你喜欢
  • 2017-11-12
  • 2018-10-02
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 2018-02-23
  • 2018-10-02
  • 1970-01-01
相关资源
最近更新 更多