Angular 7和RxJS 6.3.3应用程序更新后的Observable存在问题
我将我的应用程序更新为Angular 7和RxJS 6.3.3,在修改代码时遇到了问题。我真的需要这方面的帮助,因为我无法找到进入谷歌或堆栈溢出的解决方案 我的进口:Angular 7和RxJS 6.3.3应用程序更新后的Observable存在问题,angular,rxjs,angular7,angular-httpclient,rxjs6,Angular,Rxjs,Angular7,Angular Httpclient,Rxjs6,我将我的应用程序更新为Angular 7和RxJS 6.3.3,在修改代码时遇到了问题。我真的需要这方面的帮助,因为我无法找到进入谷歌或堆栈溢出的解决方案 我的进口: import { Observable, of } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import { Injectable, Inject, Optional, InjectionToken } from '@angular/core';
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';
这是我的测试
功能的代码:
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);
}
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);
})
);
}
IMyResponseClass
pseudo代码:
class MyResponseClass implements IMyResponseClass {
property1?: string | undefined;
property2?: string | undefined;
property3: boolean;
...
}
interface IMyResponseClass {
property1?: string | undefined;
property2?: string | undefined;
property3: boolean;
}
这是我的get()
函数的代码:
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);
}
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);
})
);
}
get():可观察{
让url=某个url;
让选项:任意={
方法:“获取”,
标题:新标题({
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
})
};
返回此.http.request(url、选项).pipe(
map((response_u2;:any)=>{返回this.test(response_2;})),
catchError((错误:任意,捕获:可观察)=>{
如果(捕捉到响应的实例){
试一试{
把这个还给我(
以下是此错误的文本版本:
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'.
类型为“(err:any,catch:Observable)=>MyResponseClass | Observable”的参数不能分配给类型为“(err:any,catch:Observable)=>ObservableInput”的参数。
类型“MyResponseClass | Observable”不可分配给类型“ObservableInput”。
类型“MyResponseClass”不可分配给类型“ObservableInput”。
类型“MyResponseClass”不可分配给类型“Iterable”。
类型“MyResponseClass”中缺少属性“[Symbol.iterator]”。
我做错了什么?你能帮我吗
是我在那个错误之前做的前一步。也许我在前一步做错了什么
在阅读了所有注释后,我做了一些更改,因此现在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);
})
);
返回这个.http.request(url\ux,options\ux).pipe(
map((response:any)=>{返回this.test(response);}),
catchError((错误:任意,捕获:可观察)=>{
如果(捕捉到响应的实例){
试一试{
返回此。测试(不要从rxjs compat
导入可观测数据,使用rxjs
使用rxjs compat
的唯一好理由是,如果您的依赖项仍然依赖于较旧版本的rxjs
我注意到您正在从test
函数返回一个可观测值,而您可能不应该这样做
无错误示例:
检查并让我知道。好的,我卸载了这个包,然后从“rxjs”重新导入Observable。我仍然有同样的错误。这不是一个解决方案。尝试在测试函数中使用Observable.of
而不是静态of
。@Chris Tapay Observable.of在中不推荐使用rxjs@6.3.3@Chris Tapay这里有一些细节:为什么你经常使用类型断言吗?我认为是可观察的。throw(e)
可以是throwerr(e)
和of(null)
可以是of(null)
@fridoo in…\node\u modules\rxjs\internal\Observable.d.ts你可以读到“throw”是“static throw:typeof throwError;”,所以无论我使用的是Observable.throw()还是throwError(),都可以将测试函数更改为受保护的测试(响应:response):Observable{let result200:any='some value';返回(result200);}这是同样的结果。@Presto您是否将返回(this.test(catch));
更改为返回此.test(catch)
当您更改test
函数时?否则您在test
函数中有一个of
,在test
函数前面有另一个。我在读了您的评论后尝试了它。这不会影响到错误。您真的需要从test
返回一个可观察值吗?请记住Catch实际上是源可观察(Http),因此它的类型是可观察的,其中T是MyResponseClass,但您提供的是可观察的。您可以在错误中看到它,并通过将Catch:Observable
更改为Catch:Observable
来修复它。