跳到主要內容

from / fromEvent

RxJS from()fromEvent() 的包裝器,使其可以接受 ref。可在 @vueuse/rxjs 附加元件中使用。

用法

分類
匯出大小
122 B
套件
@vueuse/rxjs
上次變更
5 天前
ts
import { from, fromEvent, toObserver, useSubscription } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import { map, mapTo, takeUntil, withLatestFrom } from 'rxjs/operators'
import { shallowRef } from 'vue'

const count = shallowRef(0)
const button = shallowRef<HTMLButtonElement | null>(null)

useSubscription(
  interval(1000)
    .pipe(
      mapTo(1),
      takeUntil(fromEvent(button, 'click')),
      withLatestFrom(from(count, {
        immediate: true,
        deep: false,
      })),
      map(([curr, total]) => curr + total),
    )
    .subscribe(toObserver(count)), // same as ).subscribe(val => (count.value = val))
)
js
import { from, fromEvent, toObserver, useSubscription } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import { map, mapTo, takeUntil, withLatestFrom } from 'rxjs/operators'
import { shallowRef } from 'vue'
const count = shallowRef(0)
const button = shallowRef(null)
useSubscription(
  interval(1000)
    .pipe(
      mapTo(1),
      takeUntil(fromEvent(button, 'click')),
      withLatestFrom(
        from(count, {
          immediate: true,
          deep: false,
        }),
      ),
      map(([curr, total]) => curr + total),
    )
    .subscribe(toObserver(count)),
)

類型宣告

typescript
export declare function from<T>(
  value: ObservableInput<T> | Ref<T>,
  watchOptions?: WatchOptions,
): Observable<T>
export declare function fromEvent<T extends HTMLElement>(
  value: MaybeRef<T>,
  event: string,
): Observable<Event>

原始碼

原始碼文件

貢獻者

Anthony Fu
IlyaL
Anthony Fu
Ben Lesh
Amorites
rorry121
Curt Grimes
Alexander Karelas
DesselBane
yang
Michel Betancourt

更新日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生型別 (#4636)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundle 並清理程式碼 (#4349)
v10.2.1 於 2023/6/28
fa7ed - fix(fromEvent): 正確地終止 (#3155)

以 MIT 授權條款發布。