将应用程序更新到 Angular 7 和 RxJS 6.3.3 后 Observable 出现问题

Problem with Observable after update app to Angular 7 and RxJS 6.3.3

我将我的应用程序更新到 Angular 7 和 RxJS 6.3.3,但我在更改代码时遇到了问题。我真的需要帮助,因为我找不到 google 或堆栈溢出的解决方案。

我的导入:

import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Injectable, Inject, Optional, InjectionToken } from '@angular/core';
import { Headers, ResponseContentType, Response } from '@angular/http';
import { HttpClient } from '@angular/common/http';

这是我的 test 函数的代码:

protected test(response: Response): Observable<MyResponseClass | null> {
    const status = response.status;

    if (status === 200) {
       let result200: any = null;
       ... some code ...
       return of<MyResponseClass | null>(result200);
    }
    return of<MyResponseClass | null>(<any>null);
}

这是MyResponseClass伪代码:

class MyResponseClass implements IMyResponseClass {
    property1?: string | undefined;
    property2?: string | undefined;
    property3: boolean;

    ...
}

IMyResponseClass伪代码:

interface IMyResponseClass {
    property1?: string | undefined;
    property2?: string | undefined;
    property3: boolean;
}

这是我的 get() 函数的代码:

get(): Observable<MyResponseClass | null> {
    let url_ = some_url;

    let options_: any = {
        method: "get",
        headers: new Headers({
            "Content-Type": "application/json",
            "Accept": "application/json"
        })
    };

    return this.http.request(url_, options_).pipe(
        map((response_: any) => { return this.test(response_); })),
        catchError((err: any, caught: Observable<MyResponseClass | null>) => {
            if (caught instanceof Response) {
                try {
                    return this.test(<any>caught);
                } catch (e) {
                    return <MyResponseClass | null><any>Observable.throw(e);
                }
            } else
            return <MyResponseClass | null><any>Observable.throw(caught);
        })
    );
}

我仍然收到此错误:

这是此错误的文本版本:

Argument of type '(err: any, caught: Observable<MyResponseClass>) => MyResponseClass | Observable<MyResponseClass>' is not assignable to parameter of type '(err: any, caught: Observable<MyResponseClass>) => ObservableInput<MyResponseClass>'.
  Type 'MyResponseClass | Observable<MyResponseClass>' is not assignable to type 'ObservableInput<MyResponseClass>'.
    Type 'MyResponseClass' is not assignable to type 'ObservableInput<MyResponseClass>'.
      Type 'MyResponseClass' is not assignable to type 'Iterable<MyResponseClass>'.
        Property '[Symbol.iterator]' is missing in type 'MyResponseClass'.

我做错了什么?你能帮帮我吗?


是我在该错误之前执行的上一步。也许我在上一步有什么问题?


我在阅读所有评论后进行了更改,所以现在 get() 函数如下所示:

return this.http.request(url_, options_).pipe(
            map((response_: any) => { return this.test(response_); }),
            catchError((err: any, caught: Observable<MyResponseClass | null>) => {
                if (caught instanceof Response) {
                    try {
                        return this.test(<any>caught);
                    } catch (e) {
                        return throwError(e);
                    }
                } else
                    return throwError(caught);
            })
        );

我仍然遇到错误:

不要从 rxjs-compat 导入 o​​bservable,使用 rxjs

使用 rxjs-compat 的唯一充分理由是,如果您的依赖项仍然依赖于旧版本的 rxjs

我注意到您从 test 函数返回了一个 Observable,而您可能不应该这样做。

无错误示例https://stackblitz.com/edit/angular-zimn8f

查看并告诉我。