小编Joh*_*eer的帖子

HTTP在Angular 6中调用

我将Angular项目更新为Angular 6,并且不知道如何进行http获取请求.这就是我在Angular 5中的表现:

get(chessId: string): Observable<string> {

this.loadingPanelService.text = 'Loading...';
this.loadingPanelService.isLoading = true;

const url = `${this.apiPathService.getbaseUrl()}api/chess/${chessId}/rating`;   

return this.http.get<string>(url)
.catch((error) => {
    console.error('API error: ', error);

    this.loadingPanelService.isLoading = false;
    this.notificationService.showErrorMessage(error.message);

    return Observable.of(null);
  })
  .share()
  .finally(() => {
    this.loadingPanelService.isLoading = false;
  });
Run Code Online (Sandbox Code Playgroud)

这就是我现在正在做的事情.这是应该如何在Angular 6中完成的吗?

...
return this.http.get<string>(url)
.pipe(
  catchError(this.handleError),
  share(),
  finalize(() =>{this.loadingPanelService.isLoading = false})
);

private handleError(error: HttpErrorResponse) {
console.error('API error: ', error);

this.loadingPanelService.isLoading = false;
this.notificationService.showErrorMessage(error.message);

// return an observable with a user-facing error message
return throwError(
  'Something bad happened; …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular-httpclient angular6

5
推荐指数
1
解决办法
2万
查看次数