所以我通常会这样写我的http请求
服务
getData(){
return this.http.get('url')
}
Run Code Online (Sandbox Code Playgroud)
零件
getTheData() {
this.service.getData().subscribe(
(res) => {
//Do something
},
(err) => {
console.log('getData has thrown and error of', err)
})
Run Code Online (Sandbox Code Playgroud)
但是浏览Angular文档后,他们似乎在Service中将其格式化为
getHeroes(): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
catchError(this.handleError('getHeroes', []))
);
}
Run Code Online (Sandbox Code Playgroud)
这隐含的上行空间是什么,因为它对我来说似乎很冗长,而且我个人从不需要解决错误。