将应用程序更新到Angular 7后如何更改HttpClient代码 [英] How to change HttpClient code after update app to Angular 7

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

问题描述

我已将Angular更新到版本7,并在应用程序中进行了一些更改,但我不确定该怎么做.

I updated Angular to version 7 into my app and I have to change my code a little bit after update but I'm not sure how to do it.

这是要更改的代码示例(在Angular的早期版本中):

This is example of code to change (in previous version of Angular):

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_).flatMap((response_: any) => {
        return this.getResponse(response_);
    }).catch((response_: any) => {
        if (response_ instanceof Response) {
            try {
                return this.getResponse(<any>response_);
            } catch (e) {
                return <Observable<MyResponseClass | null>><any>Observable.throw(e);
            }
        } else
            return <Observable<MyResponseClass | null>><any>Observable.throw(response_);
    });
}

对于这个问题,getResponseMyResponseClass是什么都无所谓,所以我将省略说明.

For this question it doesn't matter what getResponse and MyResponseClass they are, so I'll skip to explain that.

好的,因此第一步是将.flatMap更改为.pipe()和map(),因为:

Ok, so the first step is changing .flatMap to .pipe() and map() because of that:

Observable< Object>类型的属性'flatMap'不存在.

Property 'flatMap' does not exist on type Observable<Object>.

因此,在进行此更改之后,我的代码如下所示:

So after this change my code looks like that:

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

现在我出现了这样的错误:

And now I've got the error like that:

类型'Observable< Observable< MyResponseClass >>'上的属性'catch'不存在.

Property 'catch' does not exist on type 'Observable<Observable<MyResponseClass>>'.

在这里,我遇到了问题,因为我真的不知道该如何更改.我仍在寻找一些信息到谷歌和堆栈溢出,但尚未成功.也许我对此不够了解.

And here I've got the problem because I really don't know what I should to change it. I'm still looking for some information into google and stack overflow but without success yet. Maybe I haven't enough knowladge of that.

您会帮我吗?

推荐答案

您的RxJS很可能已从v5.x更新到v6.x.因此,您需要重构涉及RxJS的代码.

Your RxJS is most probably updated from v5.x to v6.x. Therefore you need to refactor your code which involves RxJS.

这是更新指南:

查看全文

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