@vueuse/rxjs
これはVueUseのアドオンで、RxJSを自然な方法で使用できるようにします。
インストール
bash
npm i @vueuse/rxjs rxjs
関数
from
— RxJSのfrom()
とfromEvent()
をラップして、ref
を受け入れられるようにします。toObserver
—ref
をRxJSのObserverに変換するための簡略化関数useExtractedObservable
— 1つ以上のコンポーザブルから抽出されたRxJSのObservable
を使用します。useObservable
— RxJSのObservable
を使用します。useSubject
— RxJSのSubject
をref
にバインドし、値の変更を双方向に伝播します。useSubscription
— アンサブスクライブしたり、メモリリークを起こしたりすることなく、RxJSのSubscription
を使用します。watchExtractedObservable
— 1つ以上のコンポーザブルから抽出されたRxJSのObservable
の値を監視します。
例
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], []),
)),
),
)
ライセンス
MITライセンス © 2019-現在 Anthony Fu