【发布时间】:2020-01-03 17:32:25
【问题描述】:
错误:类型“订阅”缺少以下属性 输入“Observable>”:_isScalar、源、运算符、升降机、 还有 6 个.ts(2740)
这里我附上了我的代码。
在这里,在我的例子中,我有两个方法可以返回一个 observable,但是 getByTypeData 和 getByType。但是,从 getByTypeData() 返回 this.getByType(type).. 时,我遇到了上述错误。
P.S.:我想在我的组件中订阅 getByTypeData,它应该返回一个 observable。而且我是 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);
}
【问题讨论】:
-
你说你会返回一个 observable,但返回的是一个订阅。不要那样做。
-
嗨@jonrsharpe,你能详细解释一下吗。可能是通过我的代码?谢谢。
-
好吧,当你写
: Observable<stringMap<any>>时,这意味着 “这个函数将返回一个 observable”,但随后return this.getByType(type).subscribe(...)返回一个 subscription,它不是可观察的。要么改变你说你要返回的东西,要么改变你正在返回的东西。
标签: javascript angular rxjs observable subscription