我正在向响应的外部API发出HTTP请求:
{
id: 1,
user_id: 1,
name: "Fake Tournoi",
slug: "fake-tournoi",
dateIni: "2018-06-13 00:00:00",
dateFin: "2018-06-14 00:00:00",
registerDateLimit: "2018-06-10 00:00:00",
sport: 1,
promoter: "promoter2",
host_organization: "host2",
technical_assistance: "tech2",
rule_id: 1,
type: 0,
venue_id: 1,
level_id: 7,
created_at: "2018-06-02 18:56:44",
updated_at: "2018-06-12 10:55:46",
deleted_at: null,
championships: [
{...},
{...},
{...},
{...}
]
}
因此,这里的主要对象是a Tournament
,然后冠军是一组Championship
对象
我以为我在使用以下方法获取数据时正在投射对象:
getTournamentWithTrees(): Tournament {
this.loading = true;
this.treeService.getTournamentWithTrees(this.slug)
.pipe(first())
.subscribe(tournament => {
this.tournament = tournament;
this.loading = false;
}, err => {
this.loading = false;
});
return null;
}
这个方法应该有一个Tournament
类型,然后在Tournament
定义中,我有:
export class Tournament {
id: number;
...
competitors: Competitor[];
}
当我尝试对以下Tournament
对象使用对象或对对象使用方法Championship
时:
<div *ngIf="championship.hasPreliminary()">
has Prelim
</div>
我收到一个错误:
ERROR TypeError: _v.parent.context.$implicit.hasPreliminary is not a function
我猜这是因为我的冠军数据不被视为冠军对象。我应该如何解决呢?
使用这样的构造函数:
export class Tournament {
constructor(payload: Object) {
Object.assign(this, payload);
}
}
并这样称呼它:
this.http.get('...')
.map(tournaments => tournaments
.map(tournament => new Tournament(tournament))
);
发出HTTP请求时,不会键入有效内容:为HTTP调用提供类只会启用IDE的自动完成功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句