大話西游666
2021-09-30 13:57:56
錯誤:“訂閱”類型缺少“可觀察>”類型中的以下屬性:_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); }
1 回答

三國紛爭
TA貢獻1804條經驗 獲得超7個贊
正如評論中指出的那樣,您返回的是 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);
})
);
}
switchMap 是一個需要使用如下語句導入的 rxjs 運算符:
import { switchMap } from 'rxjs/operators'
可以在此處找到有關此運算符的文檔
一篇解釋映射運算符的好文章在這里
添加回答
舉報
0/150
提交
取消