@vueuse/rxjs
这是 VueUse 的一个附加组件,它提供了一种自然的方式来使用 RxJS。
安装
bash
npm i @vueuse/rxjs rxjs函数
from— 对 RxJS 的from()和fromEvent()的封装,允许它们接受reftoObserver— 语法糖函数,用于将ref转换为 RxJS ObserveruseExtractedObservable— 使用从一个或多个组合式函数中提取的 RxJSObservableuseObservable— 使用 RxJSObservableuseSubject— 将 RxJSSubject绑定到ref并双向传播值变化useSubscription— 使用 RxJSSubscription,无需担心取消订阅或造成内存泄漏watchExtractedObservable— 监听从一个或多个组合式函数中提取的 RxJSObservable的值
示例
ts
import { from, fromEvent, useObservable } from '@vueuse/rxjs'
import { forkJoin, of } from 'rxjs'
import { ajax } from 'rxjs/ajax'
import { concatAll, map, mergeMap, pluck, scan, take } from 'rxjs/operators'
import { useTemplateRef } from 'vue'
const BASE_URL = 'https://jsonplaceholder.typicode.com'
const button = useTemplateRef('buttonRef')
const posts = useObservable(
fromEvent(button, 'click').pipe(
mergeMap(() => ajax.getJSON(`${BASE_URL}/posts`).pipe(
concatAll(),
take(4),
mergeMap(({ id, userId, title }) => forkJoin({
id: of(id),
comments: ajax.getJSON(`${BASE_URL}/posts/${id}/comments`).pipe(
map(comments => comments.length),
),
username: ajax.getJSON(`${BASE_URL}/users/${userId}`).pipe(
pluck('username'),
),
}), 2),
scan((acc, curr) => [...acc, curr], []),
)),
),
)许可证
MIT 许可证 © 2019-PRESENT Anthony Fu