官方的Angular HttpClient文档提供了以下示例,用于向后端服务器发起POST请求:。
/** POST: add a new hero to the database */
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
但我对于Observable返回英雄数据或者其他数据而不是成功代码感到困惑,尤其是在POST请求的时候。我知道为什么GET请求需要类型断言,但是不太理解它是如何工作的。