问题描述
我已将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_);
});
}
对于这个问题,getResponse
和MyResponseClass
是什么都无所谓,所以我将省略说明.
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:
因此,在进行此更改之后,我的代码如下所示:
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:
在这里,我遇到了问题,因为我真的不知道该如何更改.我仍在寻找一些信息到谷歌和堆栈溢出,但尚未成功.也许我对此不够了解.
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
.
这是更新指南:
https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md
这篇关于将应用程序更新到Angular 7后如何更改HttpClient代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!