How to save a data array object to ionic SQlite storage (TypeScript, Angular 5, Ionic 3)

前端 未结 1 677
有刺的猬
有刺的猬 2021-01-28 05:00

I\'m trying to save a data array object with many attributes to a favorite list using Ionic SQlite storage.

For instance, I have a data array in data provider:

相关标签:
1条回答
  • 2021-01-28 05:07

    make a function to save the favorites:

    addFavorite(favorite:any):void{
      let favorites = this.storage.get('favorites');
      if(favorites){
        favorites.push(favorite);
        this.storage.set('favorites', favorites);
      }else{
        favorites = [];
        favorites.push(favorite);
        this.storage.set('favorites',favorites);
      }
    

    then to get favorites add this function:

    getFavorites():any{
      let favorites = this.storage.get("favorites");
      return favorites;
    }
    

    for ionic: if you are using @ionic/storage then to load items use async pipe:

    getFavorites():Promise<any>{
      let favorites = this.storage.get("favorites");
      return favorites;
    }
    

    then to access them in *ngFor do this:

    <div *ngFor="let item of getFavorites() | async">
          <div>{{item.name}}</div>
    </div>
    
    0 讨论(0)
提交回复
热议问题