Ionic 6迭代形式本地存储

vfhzx4xs  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(218)

Hi there guys i fetch form my Laravel backend a object with some devices associated to logged user. I store the data into my Ionic localstorage in this way:

login(user: User): Observable<AuthResponse> {
return this.http.post(`${this.apiURL}/login`, user).pipe(
  tap(async (res: AuthResponse) => {
    console.log('res', res);
      await this.storage.set("ACCESS_TOKEN", res['data']['token']);
      await this.storage.set("id", res['data']['id']);
      await this.storage.set("devices", res['data']['devices']);

      console.log(this.authSubject);
      this.authSubject.next(true);
  })
);

}
SO now i need to create a ion-slides foreach devices that i fetch after login. I tried in my NgInit:

ngOnInit() {
this.storage.get("devices").then((value) => 
{
  console.log('devices', value);
    let devices = value;
});

}
but didn't work. I need to show devices as slideshow in my view:

<ion-slides >
<ion-slide *ngFor="let device of this.devices">
  <ion-row>
  <h1>{{ device.name }}</h1>
  </ion-row>
  <ion-row>
    <img src="{{ device.image }}" >
  </ion-row>
</ion-slide>
wsewodh2

wsewodh21#

在那里,您应该将devices绑定到组件类的this上下文,而不是在let变量中

let devices = value

this.storage.get("devices").then((value) => {
  console.log('devices', value);
  this.devices = value
});

在HTML中,尽管使用this.devices,但仍使用devices

<ion-slide *ngFor="let device of devices">

相关问题