
在本文中,我们将讨论RxJS 6.5.0最新版本的新功能和改进。
新的fromFetch语句
RxJS现在提供了对本地提取 API的本地支持,包括使用AbortController中断请求的功能 。
import { of } from 'rxjs'; import { switchMap, catchError } from 'rxjs/operators'; import { fromFetch } from 'rxjs/fetch'; const users$ = fromFetch('https://reqres.in/api/users').pipe( switchMap(response => { if (response.ok) { return response.json(); } else { return of({ error: true, message: `Error ${response.status}` }); } }), catchError((error) => of({ error: true, message: error.message })) ); users$.subscribe({ next(data) { ... }, complete() { ... } });
增强的forkJoin语句
我最喜欢它。 forkJoin语句现在接受源字典:
import { forkJoin, timer } from 'rxjs'; import { take, mapTo } from 'rxjs/operators'; const source = forkJoin({ todos: timer(500).pipe(mapTo([{ title: 'RxJS'}])), user: timer(500).pipe(mapTo({ id: 1 })) }); source.subscribe({ next({ todos, user }) { } });
另外,不建议使用forkJoin(a, b, c, d)
形式的此运算符;相反,应编写forkJoin([a, b, c, d])
。
分区可观察
现在不推荐使用现有的partition
语句,并使用新的通用可观察的partition
语句partition
。
分区将源对象分为两个可观察类型的对象,一个用于满足给定谓词的值,第二个用于不满足条件的值。
import { fromEvent, partition } from 'rxjs'; const clicks$ = fromEvent(document, 'click'); const [clicksOnH1$, clicksElsewhere$] = partition(clicks$, event => event.target.tagName === 'H1'); clicksOnH1$.subscribe({ next() { console.log('clicked on h1') } }); clicksElsewhere$ .subscribe({ next() { console.log('Other clicked') } });
CombineLatest已转为已弃用状态
新版本不再使用combineLatest
签名,但combineLatest([a, b, c])
。 在此处阅读更改的原因。
策划者
使用scheduled
语句为现有可观察项配置调度程序。 不推荐使用from
, range
和其他语句的计划版本(将调度程序作为参数的版本)。
import { of, scheduled, asapScheduler } from 'rxjs'; console.log(1);