IONIC 5找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到数组等Iterables

时间:2020-05-20 16:45:27

标签: angular ionic-framework

尝试从ionic 5应用程序的API获取数据时遇到此错误。我尝试删除所有数组,但无法正常工作。有人可以帮忙吗?

这是我的文件:

types.ts

export interface Activity {
    area_metadata: Areametadatum[];
    items: Item[];
    api_info: Apiinfo;
}

export interface Apiinfo {
    status: string;
}

export interface Item {
    update_timestamp: string;
    timestamp: string;
    valid_period: Validperiod;
    forecasts: Forecast[];
}

export interface Forecast {
    area: string;
    forecast: string;
}

export interface Validperiod {
    start: string;
    end: string;
}

export interface Areametadatum {
    name: string;
    label_location: Labellocation;
}

export interface Labellocation {
    latitude: number;
    longitude: number;
}

api.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Activity } from '../types';

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(private _httpClient: HttpClient) {}

  getAllActivities(): Observable<Activity> {
    return this._httpClient.get<Activity>(API);
  }
}

const API = "https://api.data.gov.sg/v1/environment/2-hour-weather-forecast";

home.page.ts

import { Component, OnInit } from '@angular/core';
import { AuthenticationService } from "../authentication/authentication-service";
import { Platform } from '@ionic/angular';
import { Router } from '@angular/router';
import { FormBuilder } from '@angular/forms';
import { Observable } from 'rxjs';
import { Activity } from '../types';
import { ApiService } from '../services/api.service';


@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {

  activityList: Observable<Activity>;

  constructor(apiService: ApiService) {
    this.activityList = apiService.getAllActivities();
  }

  ngOnInit() {
  }
}

home.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>Home</ion-title>
    <ion-buttons slot="start">
      <ion-menu-button menu="main-menu"></ion-menu-button>
    </ion-buttons>
  </ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true" class="ion-padding">
  <ion-grid>
    <ion-row>
      <ion-col>
        <ion-card button *ngFor="let activity of activityList">
          <ion-card-header>
            <ion-card-title>{{activity.items}}</ion-card-title>
          </ion-card-header>
        </ion-card>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

1 个答案:

答案 0 :(得分:0)

选项1:订阅

getAllActivities()函数返回一个可观察值。您需要订阅它并在订阅中分配局部变量。

export class HomePage implements OnInit {

  activityList = [];        // <-- type should be array not `observable`

  constructor(apiService: ApiService) {
    apiService.getAllActivities().subscribe(    // <-- subscribe here
      response => {
        this.activityList = response;
      },
      error => {
        // always good practice to handle error in HTTP observables
      }
    );
  }

  ngOnInit() {
  }
}

选项2:async管道

或者,您可以将activityList保留为observable,并在模板中包含async管道。

但是将可观察的类型更改为any。实际上它将是Angular HTTP可观察的。

控制器

export class HomePage implements OnInit {
  activityList: Observable<any>;      // <-- change the type to `any`

  constructor(apiService: ApiService) {
    this.activityList = apiService.getAllActivities();
  }

  ngOnInit() {
  }
}

模板

<ion-card button *ngFor="let activity of activityList | async">
  <ion-card-header>
    <ion-card-title>{{activity?.items}}</ion-card-title>
  </ion-card-header>
</ion-card>