繁体   English   中英

类型 [] 上不存在属性

[英]Property does not exist on type []

我尝试编译我的 PWA,并且在 ionic-serve 上的 localhost 上一切正常,但是当我尝试编译时,我在 dev = [] 上遇到了类型错误。 这是我在 device.page.ts 上的代码

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 服务中的 function :

    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 '{}'.

有人可以帮我理解吗?

您的dev class 属性已初始化为空数组= [] ,您正在尝试从dev数组访问messages属性...

另外,我强烈建议(为了您自己的利益)停止使用any数据类型并开始创建自己的数据类型,否则您最终将在 typescript 中拥有一个普通的typescript项目......

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM