Я пытался сделать вызов API, но безуспешно.Я получаю это сообщение об ошибке при отображении имени.Очень жаль, что я вставил три файла, но это мой первый вопрос, и я новичок.Спасибо за любую помощь.
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
Вот мой beers.service.ts
файл
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class BeersService {
private beerUrl = 'https://api.punkapi.com/v2/beers/';
beers;
constructor(private _httpClient: HttpClient) { }
getListOfBeer() {
return this._httpClient.get(this.beerUrl).subscribe(
res => {
this.beers = res;
});
}
}
Вот мой app.component.ts
У меня такое чувство, что проблема сконструктор, но все, что я пробовал, не привело к успеху.
import { Component } from '@angular/core';
import { BeersService } from './beers.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
beers;
constructor(private _beerService: BeersService) {
this.beers = _beerService.getListOfBeer()
}
}
И, наконец, вот мой app.component.html
<div class="container">
<div *ngFor="let beer of beers">
<p>{{ beer.name }}</p>
</div>
</div>