I need some help in Rxjs Operators. I'm working in Angular 6 Project. Rxjs version is 6.1.0 I was facing the issue in Rxjs Operator importing. So I execute this statement with hope to resolve my issues.
Statement: npm install --save rxjs-compat
And here is my component code.
import { AppError } from './../common/validators/app-error';
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from "rxjs";
import { catchError, map } from "rxjs/operators";
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class PostService {
deletePost(id) {
return this.http.delete(this.url + '/' + id)
.pipe(
map(res => res),
catchError((error: HttpErrorResponse) => {
Observable.throw(new AppError());
}));
}
private url = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: Http) { }
}
And I'm getting this error with compiled successfully.
i 「wdm」: Compiled successfully.
ERROR in src/app/services/post.service.ts(18,20): error TS2345: Argument of type '(error: HttpErrorResponse) => void' is not assignable to parameter of type '(err: any, caught: Observable<Response>) => ObservableInput<{}>'.
Type 'void' is not assignable to type 'ObservableInput<{}>'.
How can I use Rxjs successfully? What's the issue here.
You have to use catchError
within the pipe
function now, like so:
deletePost(id) {
return this.http.delete(this.url + '/' + id)
.pipe(
map(res => res),
catchError((error: HttpErrorResponse) => {
return Observable.throw(new AppError());
}));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.