将应用程序更新到Angular 7和RxJS 6.3.3后可观察到的问题 [英] Problem with Observable after update app to Angular 7 and RxJS 6.3.3

查看:83
本文介绍了将应用程序更新到Angular 7和RxJS 6.3.3后可观察到的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的应用程序更新为Angular 7和RxJS 6.3.3,在对代码进行更改时遇到了问题.我真的需要帮助,因为我无法找到Google解决方案或堆栈溢出.

I updated my app to Angular 7 and RxJS 6.3.3 and I've got a problems during making changes into my code. I really need help with that, because I can't to find solution into google or stack overflow.

我的进口商品:

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

我仍然收到此错误:

这是此错误的文本版本:

Here is a text version of this error:

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

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

What I'm doing wrong? Will you help me?

此处是我在该错误之前所做的上一步.也许在上一步中我出了什么问题?

Here is the previous step which I did before that error. Maybe did I something wrong on previous step?

我在阅读所有注释后进行了更改,因此现在的get()函数如下所示:

I made a changes after read all of comments so now the get() function looks like that:

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

我仍然遇到错误:

推荐答案

我注意到您可能不应该从test函数返回一个Observable.

I noticed you're returning an Observable from the test function, when you probably shouldn't.

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

检查并让我知道.

这篇关于将应用程序更新到Angular 7和RxJS 6.3.3后可观察到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆