Javascript RXJS 6处理http可观察对象的方式是什么?

Javascript RXJS 6处理http可观察对象的方式是什么?,javascript,angular,typescript,ecmascript-6,rxjs,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/ser

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

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'))
    );

  }
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“rxjs”导入{Observable};
从'@app/constants'导入{TypeAndCountURL};
从'@app/services/http error.service'导入{HttpErrorService};
从“@app/models/type和count/body.interface”导入{TypeAndCountResponseBody,TypeAndCountRequestBody};
@注射的({
providedIn:'根'
})
出口级服务{
构造函数(私有http:HttpClient,私有httpErrorService:httpErrorService){}
postTypeAndCountRequest(typeAndCountRequestBody:typeAndCountRequestBody):可观察{
返回此.http.post(TypeAndCountURL,typeAndCountRequestBody).pipe(
catchError(this.httpErrorService.handleError('postTypeAndCountRequest'))
);
}
}
具体来说,我发现
找不到名称“catchError”。您的意思是“RTCError”吗?ts(2552)

读了这篇文章,我发现我可以通过单独导入来解决这个问题(从rxjs/operators..这很好,但是。。而且整个结构是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
       );
    }

自rxjs 6以来,所有操作员都必须单独导入。从“rxjs/operators”导入{catchError};我还需要用它吗?我读到整个操作符都在弃用的路径上……您可以删除catchError操作符并在订阅级别处理错误。e、 g:myobservable.subscribe(result=>{…do something},error=>{…handle error})我在我的OP中注意到,我知道我可以这样做,但在其他地方,这被描述为一种“旧”的方式。“我在寻求新的方法。”罗伯托·托马斯再次检查我的答案。你可以像我一样处理错误。第一次回调是result,第二次回调是error handler嘿,托尼,我再次感谢你的回答——我注意到它变得越来越流行:)你可能想修改它,以淡化旧的方式,因为这不是你文章的实质内容。最好的