类型 'Subscription' 缺少类型 'Observable<StringMap<any>>' 的以下属性
Type 'Subscription' is missing the following properties from type 'Observable<StringMap<any>>'
ERROR : Type 'Subscription' is missing the following properties from
type 'Observable>': _isScalar, source, operator, lift,
and 6 more.ts(2740)
这里附上我的代码。
在这里,就我而言,我有两个方法,return 一个可观察的方法,但是 getByTypeData 和 getByType。但是,在 returning this.getByType(type).. from getByTypeData() 上,我遇到了上述错误。
P.S.: 我想在我的组件中订阅 getByTypeData,它应该 return 我一个可观察的。我是 RXJS 的新手...
/*
interface IStringMap<T> {
[index: string]: T;
}
*/
getByTypeData(type: string, ignoreApi = false): Observable<stringMap<any>> {
if (ignoreApi) {
this.handleConfig(type);
}
return this.getByType(type)
.subscribe(response => {
const config = response.result ? response.data : {};
return this.handleConfig(type, config);
});
}
// This method in another file (Just for reference)
getByType(type: string): Observable<stringMap<any>> {
return this.httpClient.get(`get url`);
}
handleConfig(type: string, config: stringMap<string | number> = {}): Observable<stringMap<any>> {
if (type === this.types) {
config.token = this.anotherservice.GetKey('mykey');
if (config.token) {
// logic
}
}
if (type === this.types) {
// logic
}
return of(config);
}
正如评论中所指出的,您返回的是 Subscription
而不是 Observable
。我建议您阅读 documentation 以更好地了解它们之间的区别。
在您的具体情况下,我建议您改为尝试以下操作:
getByTypeData(type: string, ignoreApi = false): Observable<stringMap<any>> {
if (ignoreApi) {
return this.handleConfig(type);
}
return this.getByType(type).pipe(
switchMap(response => {
const config = response.result ? response.data : {};
return this.handleConfig(type, config);
})
);
}
switchMap
是一个 rxjs 运算符,需要用这样的语句导入:
import { switchMap } from 'rxjs/operators'
ERROR : Type 'Subscription' is missing the following properties from type 'Observable>': _isScalar, source, operator, lift, and 6 more.ts(2740)
这里附上我的代码。
在这里,就我而言,我有两个方法,return 一个可观察的方法,但是 getByTypeData 和 getByType。但是,在 returning this.getByType(type).. from getByTypeData() 上,我遇到了上述错误。
P.S.: 我想在我的组件中订阅 getByTypeData,它应该 return 我一个可观察的。我是 RXJS 的新手...
/*
interface IStringMap<T> {
[index: string]: T;
}
*/
getByTypeData(type: string, ignoreApi = false): Observable<stringMap<any>> {
if (ignoreApi) {
this.handleConfig(type);
}
return this.getByType(type)
.subscribe(response => {
const config = response.result ? response.data : {};
return this.handleConfig(type, config);
});
}
// This method in another file (Just for reference)
getByType(type: string): Observable<stringMap<any>> {
return this.httpClient.get(`get url`);
}
handleConfig(type: string, config: stringMap<string | number> = {}): Observable<stringMap<any>> {
if (type === this.types) {
config.token = this.anotherservice.GetKey('mykey');
if (config.token) {
// logic
}
}
if (type === this.types) {
// logic
}
return of(config);
}
正如评论中所指出的,您返回的是 Subscription
而不是 Observable
。我建议您阅读 documentation 以更好地了解它们之间的区别。
在您的具体情况下,我建议您改为尝试以下操作:
getByTypeData(type: string, ignoreApi = false): Observable<stringMap<any>> {
if (ignoreApi) {
return this.handleConfig(type);
}
return this.getByType(type).pipe(
switchMap(response => {
const config = response.result ? response.data : {};
return this.handleConfig(type, config);
})
);
}
switchMap
是一个 rxjs 运算符,需要用这样的语句导入:
import { switchMap } from 'rxjs/operators'