繁体   English   中英

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

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

我需要Rxjs运算符中的一些帮助。 我正在Angular 6 Project中工作。 Rxjs版本是6.1.0,我在Rxjs操作员导入中遇到了问题。 因此,我执行该声明希望能解决我的问题。

Statement: npm install --save rxjs-compat

这是我的组件代码。

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) { }

}

我得到编译成功的错误。

    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<{}>'.

如何成功使用Rxjs? 这是什么问题。

您现在必须在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