Skip to content

@vueuse/rxjs

这是 VueUse 的插件,支持以自然的方式使用 RxJS。

¥This is an add-on of VueUse, enabling a natural way of using RxJS.

安装

¥Install

bash
npm i @vueuse/rxjs rxjs

函数

¥Functions

示例

¥Example

ts
import { from, fromEvent, useObservable } from '@vueuse/rxjs'
import { ref } from 'vue'
import { forkJoin, of } from 'rxjs'
import { ajax } from 'rxjs/ajax'
import { concatAll, map, mergeMap, pluck, scan, take } from 'rxjs/operators'

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 许可证 © 2019-PRESENT Anthony Fu

¥MIT License © 2019-PRESENT Anthony Fu

VueUse 中文网 - 粤ICP备13048890号