亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“訂閱”類型缺少“Observable<StringMap<any>>”類型中的以下屬性

“訂閱”類型缺少“Observable<StringMap<any>>”類型中的以下屬性

大話西游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'

可以在此處找到有關此運算符的文檔

一篇解釋映射運算符的好文章在這里


查看完整回答
反對 回復 2021-09-30
  • 1 回答
  • 0 關注
  • 350 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號