【问题标题】:get data ( favourite item ) from localstorage从本地存储中获取数据(最喜欢的项目)
【发布时间】:2019-08-18 04:53:13
【问题描述】:

如何从列表项应用中获取最喜欢的项目。我正在使用离子 4 。我的 json 数据添加了本地存储,但没有正确获取添加的数据。

favourite.service.ts

getAllFavoriteItems(){
    return  this.storage.get("object");
  }

  isFavorite(favorite) {
    return this.getAllFavoriteItems().then(result => {
      return result && result.indexOf(favorite) !== -1;
    });
  }

  favoriteItem(favorite) {
    return this.getAllFavoriteItems().then(result => {
      if (result) {
        result.push(favorite);
        return this.storage.set("object", result, );
      } else {
        return this.storage.set("object", [favorite]);
      }
    });
  }

  unfavoriteItem(favorite) {
    return this.getAllFavoriteItems().then(result => {
      if (result) {
        var index = result.indexOf(favorite);
        result.splice(index, 1);
        return this.storage.set("object", result);
      }
    });
  }

favourite.page.ts

  ngOnInit() {

    this.storage.get("object").then((ids) => {
      console.log( 'favouriteIds' ,ids)
      this.favouriteIds = ids;
    })


    // this.favouriteIds = this.favoriteService.getAllFavoriteItems();
    // console.log('favitems',this.favitems)
}

favourite.page.html

<ion-content no-padding color="secondary">
<ion-list> 
  <div class="main">
      <ion-item  *ngFor="let item of favouriteIds" [routerLink]="['/','details',item.id]" 
      class="item-native" (click)="showInterstitial()">
        <ion-thumbnail slot="start">
          <img [src]="item.thumnailUrl">
        </ion-thumbnail>

        <ion-label>{{item.title}}</ion-label>
        <ion-label>{{item.id}}</ion-label>

      </ion-item>

 </div>


</ion-list>
</ion-content>

屏幕截图中的完整列表项

最喜欢的列表项在这个,但没有得到正确的最喜欢的项目

【问题讨论】:

  • 第一张图片是什么?物品从哪里来……你在哪里设置收藏夹?第一个控制台日志是从什么时候开始执行的?请提供minimal reproducible example
  • home.page.ts : (第 1 页) ngOnInit(){ this.recipiservice.getData().subscribe(results =&gt;{ this.items = results; }); } recipi.service.ts :(第 2 页) getData(){ let data: Observable&lt;any&gt; = this.http.get(this.url); return data; }跨度>
  • 并没有真正回答我的任何问题。请创建一个minimal reproducible example,最好是创建一个stackblitz,然后您还需要什么代码来重现该问题:)
  • 遗憾的是 stackblitz 不起作用......

标签: javascript angular ionic-framework ionic4


【解决方案1】:

从本地存储中检索后,您需要将其解析为 JSON,

 this.favouriteIds = JSON.parse(ids);

【讨论】:

  • 请提供更多详细信息 .tnx for ans
  • 什么意思?
  • 发布console.log(ids)的输出是什么;
  • ngOnInit() { this.storage.get("object").then((ids) => { this.favouriteIds = JSON.parse(ids); console.log( 'favouriteIds' , ids) }) }
  • 这似乎不像一个数组
猜你喜欢
  • 2021-11-27
  • 2015-06-26
  • 1970-01-01
  • 2021-05-14
  • 2021-02-07
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多