我正在使用这种方法来获取数据:
home.service.ts
getHomes(): Observable<HomeApi> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'token': `${this.global.token}`
})
};
return this.http.get<HomeApi>('url', httpOptions);
}
Run Code Online (Sandbox Code Playgroud)
home.component.ts
ngOnInit() {
this.service.getHomes().subscribe((res: HomeApi) => this.home = {
images: res['images']
});
}
Run Code Online (Sandbox Code Playgroud)
主页.component.html
getHomes(): Observable<HomeApi> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'token': `${this.global.token}`
})
};
return this.http.get<HomeApi>('url', httpOptions);
}
Run Code Online (Sandbox Code Playgroud)
现在item.name工作得很好,但在控制台中抛出此错误:
错误类型错误:无法读取未定义的属性“图像”
这是示例