简体   繁体   English

如何在Angular的Rxjs 6中使用运算符

[英]How to use operator in Rxjs 6 in Angular

I need some help in Rxjs Operators. 我需要Rxjs运算符中的一些帮助。 I'm working in Angular 6 Project. 我正在Angular 6 Project中工作。 Rxjs version is 6.1.0 I was facing the issue in Rxjs Operator importing. Rxjs版本是6.1.0,我在Rxjs操作员导入中遇到了问题。 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? 如何成功使用Rxjs? What's the issue here. 这是什么问题。

You have to use catchError within the pipe function now, like so: 您现在必须在pipe函数中使用catchError ,如下所示:

deletePost(id) {
  return this.http.delete(this.url + '/' + id)
    .pipe(
       map(res => res),
       catchError((error: HttpErrorResponse) => {
         return Observable.throw(new AppError());
       }));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM