我正在学习如何在Angular 4.3中使用HTTPClientModule 我已经正确导入app.module.ts中,并且正在尝试创建一个http请求GET。这是我的app.component.ts错误TypeError:无法读取未定义的属性'名称'
import { Component, OnInit } from '@angular/core';
import { HttpClient} from '@angular/common/http';
interface Card {
card: [{
cardClass: string,
cost: number;
}];
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.http.get<Card>('https://api.hearthstonejson.com/v1/21517/enUS/cards.collectible.json').subscribe(data => {
console.log(data.card); //This is not working returning undefined
console.log(data); //This is not working (removing <Card>)
});
}
}
为什么data.card是不确定的?我怎样才能访问对象的元素,然后传入一个卡片数组? 感谢您的任何帮助
你可以发布你的HTML模板的问题吗? –
在我的角度项目中,我使用Http而不是HttpClient,也许它可以帮助你? –
Http(或将会)不推荐使用,HttpClient是现在的方式... –