javascript - RXJS 6 处理 http Observables 的方式是什么?

标签 javascript angular typescript ecmascript-6 rxjs

我注意到当我在新项目中使用我的代码时它不起作用:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import { TypeAndCountURL } from '@app/constants';
import { HttpErrorService } from '@app/services/http-error.service';
import { TypeAndCountResponseBody, TypeAndCountRequestBody } from '@app/models/type-and-count/bodies.interface';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  constructor (private http: HttpClient, private httpErrorService: HttpErrorService) {}

  postTypeAndCountRequest(typeAndCountRequestBody: TypeAndCountRequestBody): Observable<TypeAndCountResponseBody> {
    return this.http.post<TypeAndCountResponseBody>(TypeAndCountURL, typeAndCountRequestBody).pipe(
      catchError(this.httpErrorService.handleError<TypeAndCountResponseBody>('postTypeAndCountRequest'))
    );

  }
}

具体来说,我收到 Cannot find name 'catchError'。您是说“RTCError”吗?ts(2552)

读到这里,我发现我可以通过单独导入它来解决这个问题(从 rxjs/operators ..whihc 很好,但是)..而且整个结构是 rxjs 5 的兼容模式...... RXJS 6 处理错误响应的方式是什么?

最佳答案

你可以使用

import { catchError } from "rxjs/operators";

fetchUser() {
       this.userService.getProfile()
          .subscribe(
             (data: User) => this.userProfile = { ...data }
              , // success path
              error => this.error = error // error path
       );
    }

关于javascript - RXJS 6 处理 http Observables 的方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56581657/

相关文章:

javascript - 更改背景大小需要新的背景位置,有什么解决方法吗?

Angular 4 : Observable not working

css - 如何从导入的 SCSS 扩展一个类?

typescript - TypeScript 中所有重载参数类型的类型

javascript - JavaScript 的 "new"关键字是否被认为是有害的?

javascript - JSON 解析的别名字段

javascript - 无法使用javascript显示图像

angular - 手动设置 FormBuilder 控件值时出现 DOMException

用于 es6 的 Angular Polyfill

typescript - TypeScript 中的 `extends A` 和 `extends typeof A` 有什么区别?