@vueuse/rxjs
This is an add-on of VueUse, enabling a natural way of using RxJS.
Install
bash
npm i @vueuse/rxjs rxjs
Functions
from
— rxJS 的from()
和fromEvent()
的包装器,使它们能够接受ref
。toObserver
— sugar function to convert aref
into an RxJS ObserveruseExtractedObservable
— 从一个或多个组合式中提取并使用 RxJSObservable
,返回一个ref
,并在组件卸载时自动取消订阅。useObservable
— 使用 RxJSObservable
,返回一个ref
,并在组件卸载时自动取消订阅。useSubject
— 将 RxJSSubject
绑定到一个ref
上,并在两者之间传播值变化。useSubscription
— 使用 RxJSSubscription
,无需担心取消订阅或创建内存泄漏。watchExtractedObservable
— 监视从一个或多个组合式中提取的 RxJSObservable
的值。
Example
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 { ref } from 'vue'
const BASE_URL = 'https://jsonplaceholder.typicode.com'
const button = ref<HTMLButtonElement>(null)
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], []),
)),
),
)
License
MIT License © 2019-PRESENT Anthony Fu