【发布时间】:2019-06-23 18:55:29
【问题描述】:
我将我的应用更新到 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 { 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'.
我做错了什么?你会帮我吗?
Here 是我在该错误之前执行的上一步。也许我在上一步有什么问题吗?
我在读取所有 cmets 后进行了更改,所以现在 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);
})
);
我仍然收到错误:
【问题讨论】:
-
尝试在你的测试函数中使用
Observable.of而不是静态of。 -
@Chris Tapay Observable.of 在 rxjs@6.3.3 中已弃用
-
@Chris Tapay 这里有一些细节:github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/…
-
你为什么经常使用类型断言?我认为
<MyResponseClass | null><any>Observable.throw(e)可能只是throwError(e)和of<MyResponseClass | null>(<any>null)可能只是of(null)。 -
@fridoo in ...\node_modules\rxjs\internal\Observable.d.ts 你可以读到'throw'是'static throw: typeof throwError;'所以不管我使用的是 Observable.throw() 还是 throwError()
标签: angular rxjs angular7 angular-httpclient rxjs6