[英]Observable `of` deprecated. What is the equivalent?
就我而言,我有一個訪問令牌,如果該令牌存在,我會將它作為字符串類型的可觀察對象返回:
if (this.accessToken){
return of(this.accessToken);
}
由於最近的更新,我注意到of
已棄用,並顯示以下消息:
of is deprecated: use scheduled instead 'scheduled([a, b, c], scheduler)' (deprecation)
新語法非常冗長,有人知道相同簡單of
的等效scheduled
版本嗎? 關鍵字名稱使得搜索相關信息變得困難。
謝謝!
只有接受調度程序的重載被棄用。 您使用的變體未棄用,請參閱https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/of.ts
如上所述,它沒有被棄用。
我想你正在從 RxJS v5遷移到 RxJS v6 。 在這種情況下:
標准的 observable 處理,如 of、map、filter 等
Observable.of(1,2,3).map(x => 2 * x);
成為
import {of, map} from 'rxjs';
import {map} from 'rxjs/operators';
of(1,2,3).pipe(map(x => 2 * x));
如果您確實有調度程序,則of(item, scheduler)
的等效of(item, scheduler)
是scheduled([item], scheduler)
。 如果您已經傳入了一組項目,則不需要括號。
@daniel-hilgarth 是對的,但如果您需要模擬of(1, 2, 3)
您可以使用以下命令
import {asapScheduler, scheduled} from "rxjs";
scheduled([1, 2, 3], asapScheduler);
或者
import {asyncScheduler, scheduled} from "rxjs";
scheduled([1, 2, 3], asyncScheduler);
您可以在此處閱讀有關 asap 的更多信息: https : //rxjs.dev/api/index/const/asapScheduler
就我而言,我有一個訪問令牌,如果該令牌存在,我將其作為字符串類型的可觀察值返回:
if (this.accessToken){
return of(this.accessToken);
}
由於最近的更新,我注意到of
是以下消息棄用:
of is deprecated: use scheduled instead 'scheduled([a, b, c], scheduler)' (deprecation)
新的語法是相當冗長,會有人知道相當於scheduled
相同的簡單版本of
? 關鍵字名稱使搜索有關它的信息變得困難。
謝謝!
您需要正確導入。 我舉以下例子供大家參考。
import { map } from 'rxjs/operators';
const httpOptions = {
headers: new HttpHeaders({
'Content-type': 'application/json'
})
};
return this.http.post(this.baseUrl + 'login', model, httpOptions).pipe(map((response : any) => {
const user = response.json();
if (user.accessToken){
localStorage.setItem('token', user.accessToken);
return user.accessToken;
} }))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.