【发布时间】:2020-02-15 03:18:28
【问题描述】:
我试图根据将通过 HTTP get 调用检索的数据加载子组件。我正在使用服务来执行 HTTP 调用并订阅组件的构造函数。但是 ngif 在数据检索之前调用。
所以我在 ngif 中应用了异步管道,但它触发了另一个错误。
服务代码:
GetContest(id){
return this.http.get(this.baseurl+id);
}
组件的构造函数:
this.service.GetContest(this.route.snapshot.paramMap.get('id')).
subscribe( (res)=> {
this.contest = res;
console.log(this.contest);
});
模板 ngif:
<app-beforecontest *ngIf="contest.startTime<date "></app-
beforecontest>
以上代码产生错误:
TypeError: Cannot read property 'startTime' of null
在此之后我像这样加入了异步管道:
<app-beforecontest *ngIf="(contest|async).startTime<date "></app-
beforecontest>
它正在生成错误:
ContestComponent.html:1 ERROR Error: InvalidPipeArgument: '[object
Object]' for pipe 'AsyncPipe'
at invalidPipeArgumentError
【问题讨论】:
-
异步管道在 observables 上工作,不清楚你为什么期望它会这样工作。
标签: angular async-await angular-httpclient angular-pipe