可观察的of of已过时。等价的是什么?

时间:2019-05-14 08:07:26

标签: angular rxjs

在我的情况下,我有一个访问令牌,并且如果该令牌存在,我将其作为字符串类型的可观察值返回:

if (this.accessToken){
  return of(this.accessToken);
}

由于最近的更新,我注意到of被以下消息淘汰:

of is deprecated: use scheduled instead 'scheduled([a, b, c], scheduler)' (deprecation)

新语法非常冗长,有人会知道同一简单scheduled的等效of版本吗?关键字名称使搜索有关它的信息变得困难。

谢谢!

6 个答案:

答案 0 :(得分:2)

仅弃用接受调度程序的重载。您使用的变体未弃用,请参见https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/of.ts

答案 1 :(得分:2)

@ daniel-hilgarth是正确的,但是如果您需要模拟- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text=[self.array objectAtIndex:indexPath.row]; if (indexPath.row ==[array count] -1 ) { //this method will get called when you will scroll to the bottom of your table view [self sendAPIRequestMethod]; } return cell; }

,则可以使用以下命令
of(1, 2, 3)

import {asapScheduler, scheduled} from "rxjs";
scheduled([1, 2, 3], asapScheduler);

您可以在此处详细了解asap:https://rxjs.dev/api/index/const/asapScheduler

答案 2 :(得分:0)

我不认为它已被弃用。您只需要像这样正确导入

import { Observable, forkJoin } from "rxjs";

下面是仍受支持的api形式rxjs

export declare function of<T, T2, T3, T4, T5, T6, T7, T8, T9>(a: T, b: T2, c: T3, d: T4, e: T5, f: T6, g: T7, h: T8, i: T9, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
export declare function of<T>(...args: (T | SchedulerLike)[]): Observable<T>;
export declare function of<T>(a: T): Observable<T>;
export declare function of<T, T2>(a: T, b: T2): Observable<T | T2>;
export declare function of<T, T2, T3>(a: T, b: T2, c: T3): Observable<T | T2 | T3>;
export declare function of<T, T2, T3, T4>(a: T, b: T2, c: T3, d: T4): Observable<T | T2 | T3 | T4>;
export declare function of<T, T2, T3, T4, T5>(a: T, b: T2, c: T3, d: T4, e: T5): Observable<T | T2 | T3 | T4 | T5>;
export declare function of<T, T2, T3, T4, T5, T6>(a: T, b: T2, c: T3, d: T4, e: T5, f: T6): Observable<T | T2 | T3 | T4 | T5 | T6>;
export declare function of<T, T2, T3, T4, T5, T6, T7>(a: T, b: T2, c: T3, d: T4, e: T5, f: T6, g: T7): Observable<T | T2 | T3 | T4 | T5 | T6 | T7>;
export declare function of<T, T2, T3, T4, T5, T6, T7, T8>(a: T, b: T2, c: T3, d: T4, e: T5, f: T6, g: T7, h: T8): Observable<T | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
export declare function of<T, T2, T3, T4, T5, T6, T7, T8, T9>(a: T, b: T2, c: T3, d: T4, e: T5, f: T6, g: T7, h: T8, i: T9): Observable<T | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
export declare function of<T>(...args: T[]): Observable<T>;

答案 3 :(得分:0)

如上所述,它不被弃用。

我想您正在从RxJS v5迁移到RxJS v6:

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));

https://www.learnrxjs.io/concepts/rxjs5-6.html

处查看更多

答案 4 :(得分:0)

如果确实有调度程序,则“ of(item,scheduler)”的等效项是“ scheduled([item],scheduler)”。如果您已经传递了一系列项目,则不需要括号。

答案 5 :(得分:0)

您需要正确导入。我举以下例子供大家参考。

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;
} }))
相关问题