The*_*ulu 7 javascript subscription observable rxjs angular
错误:“订阅”类型缺少“可观察>”类型中的以下属性:_isScalar、源、运算符、电梯和 6 个更多.ts(2740)
在这里我附上了我的代码。
在这里,就我而言,我有两种方法可以返回一个可观察对象,但是 getByTypeData 和 getByType。但是,在从 getByTypeData() 返回 this.getByType(type).. 时,我遇到了上述错误。
PS:我想在我的组件中订阅 getByTypeData 应该返回一个可观察的。我是 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);
}
Run Code Online (Sandbox Code Playgroud)
dmc*_*dle 13
正如评论中指出的那样,您返回的是 aSubscription而不是返回Observable. 我建议您阅读文档以了解它们之间的区别。
在您的特定情况下,我建议您尝试以下操作:
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);
})
);
}
Run Code Online (Sandbox Code Playgroud)
switchMap 是一个需要使用如下语句导入的 rxjs 运算符:
import { switchMap } from 'rxjs/operators'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28409 次 |
| 最近记录: |