簡體   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