Ionic 类型[]上不存在属性

5lwkijsr  于 2022-12-08  发布在  Ionic
关注(0)|答案(2)|浏览(183)

我试着编译我的PWA,在ionic-serve上的localhost上一切都正常,但是当我试着编译时,我在dev = []上得到了一个类型错误。

import { Component, OnInit } from '@angular/core';
import { MenuController } from '@ionic/angular';
import { Storage } from '@ionic/storage-angular';
import { ApiService } from '../api.service';
import { Location } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { HttpClient, HttpHeaders } from '@angular/common/http';



@Component({
  selector: 'app-device',
  templateUrl: './device.page.html',
  styleUrls: ['./device.page.scss'],
})
export class DevicePage implements OnInit {
  id: any;
  dev = [];
  data: any;
  parse: any;
  constructor(private activatedRoute:ActivatedRoute, public http:HttpClient, private storage: Storage, private api: ApiService, public menuCtrl: MenuController,
    ) {
    this.id = this.activatedRoute.snapshot.paramMap.get('id');  
  }

  ngOnInit() {
  }

  ionViewDidEnter() {
    this.api.getDeviceInfo(this.id).subscribe((response) => {
      console.log(response);
      this.dev = response;
    })
  }

  deleteDevice(id){
    alert(id);
  }

  ionViewWillEnter() {
    this.menuCtrl.enable(true);
  }
}

下面是API服务中的函数:

getDeviceInfo(id): Observable<Device[]> {
    return this.http.get<Device[]>(`${this.apiURL}/get-devices-info?id=` + id)
      .pipe(
        tap(device => console.log('Device retrieved!')),
        catchError(this.handleError<Device[]>('Get user', []))
      );
  }

      private handleError<T>(operation = 'operation', result?: T) {
        return (error: any): Observable<T> => {
          console.error(error);
          console.log(`${operation} failed: ${error.message}`);
          return of(result as T);
        };
      }

这里是我的页面:

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    
    <ion-title>{{ dev.name }} </ion-title>
    <ion-button  (click)='deleteDevice(dev.id)'>Elimina</ion-button>
  </ion-toolbar>
</ion-header>

<ion-content>
  <img src="/assets/images/{{ dev.img }}" style="width: 50%" >
  <ion-list>
    <ion-item>
      <ion-label>
        Stati e Messaggi dispositivo
      </ion-label>
    </ion-item>
    <ion-item  *ngFor="let message of dev.messages">
      <ion-label class="ion-text-wrap">
        <ion-text color="primary">
          <h3>{{ message.created_at}}</h3>
        </ion-text>
        <p>{{ message.data }}</p>
        <ion-text color="secondary">
          <p>{{ message.sequence }}</p>
        </ion-text>
      </ion-label>
    </ion-item>
  </ion-list>
</ion-content>

当我尝试编译时,我得到了错误:

Error: src/app/device/device.page.html:21:43 - error TS2339: Property 'messages' does not exist on type '{}'.

有人能帮我弄明白吗?

omvjsjqw

omvjsjqw1#

您的dev类属性已初始化为空数组= [],您正试图从dev数组访问messages属性...
此外,我强烈建议(为了您自己的利益)停止使用any,并开始创建您自己的数据类型,否则您将最终在typescript中拥有一个普通的javascript项目...

dbf7pr2w

dbf7pr2w2#

我知道,使用接口会更好。但对于那些仍然坚持这一点的人,我发现了错误:您需要使用dev ['name']而不是dev.name。

相关问题