[英]Observable<string>.switchMap is not function
我正在將應用程序從Angular 4升級到Angular 5,並在服務中包含以下代碼
loadRefData(): Observable<RefData> {
return this.participant$.switchMap((role) => {
return this.http.get(`${this.API_URL}/${role}/participantGroups/refdata`)
.map(refData => refData as RefData);
});
由於Angular 5帶有RxJS 5.5.2的最低要求,因此我將其轉換為通過.pipe()
運算符使用.pipe()
運算符。
我以為我可以做到
loadRefData(): Observable<RefData> {
return this.participant$.switchMap((role) => {
return this.http.get(`${this.API_URL}/${role}/participantGroups/refdata`)
.pipe(
map(refData => refData as RefData);
);
});
但我收到錯誤TypeError: this.participant$.switchMap is not a function
。 我沒有從打字稿編譯器得到編譯錯誤。
this.participant$
是Observable<string>
類型的,除非我弄錯了,否則我應該能夠對其執行switchMap
。
我想念什么?
這是typescript@^2.6.1
, rxjs@^5.5.2
。
編輯:我只是注意到從ngrx\\store.select
填充的this.participant$
是Store<string>
類型。
那是從Angulars博客摘錄的報價,可能對您有幫助。
HttpClient
在版本4.3中,我們以
@angular/common
形式發布了HttpClient,這是一種在Angular中進行Web請求的更小,更簡便,更強大的方法。 新的HttpClient受到了開發人員的好評,因此我們建議所有應用程序都使用HttpClient,並且不推薦使用以前的@angular/http
庫。要更新到HttpClient,您需要在每個模塊中使用
@angular/common/http
中的HttpClientModule
替換HttpModule
,注入HttpClient服務,並刪除所有map(res => res.json())
調用。不再需要。
資料來源: Angular 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.