小编Moh*_*ili的帖子

如何在 Angular 6 中绑定来自 HttpClient 的数据?

我正在使用这种方法来获取数据:

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工作得很好,但在控制台中抛出此错误:

错误类型错误:无法读取未定义的属性“图像”

这是示例

typescript angular angular6

4
推荐指数
1
解决办法
734
查看次数

标签 统计

angular ×1

angular6 ×1

typescript ×1