私はangleの新しいhttpクライアントモジュールの使い方を学んでいます。私は残りのAPIを消費しようとするとundefinedのプロパティ 'data'を読み込むことができません。 はここ Angularの新しいhttpクライアントエラー未定義のプロパティ 'data'を読み取れません
<div style="text-align:center">
<h1>
Welcome to {{title}}!
</h1>
<button (click)="getPosts()">Get Posts</button>
<div *ngFor="let result of results.data">
{{ result | json }}
</div>
</div>
はここ
import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
interface ItemsResponse {
data: any[];
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
results: any;
// Inject HttpClient into your component or service.
constructor(private http: HttpClient) {}
title = 'School2College';
ngOnInit(): void {
}
getPosts() {
this.http.get<ItemsResponse>('https://reqres.in/api/users?page=3').subscribe(res =>
this.results = res);
}
}
は何故でしょう私のapp.component.tsだ、私のapp.htmlだ未定義のプロパティ 'データ' を読み取ることができません、エラー?
安全ナビゲーションオペレータとは何ですか?もう少し説明できますか? – Karty
セーフナビゲーション演算子を使用すると、存在しないオブジェクトのオブジェクトプロパティにアクセスしようとすると、Angularがエラーをスローするのを防ぐことができます。 – Sajeetharan
何をするより良い方法?代わりにngIFを使用することもできますが、これはより良いオプションになります – Sajeetharan