我注意到当我在新项目中使用我的代码时它不起作用:
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/