跳到内容

toObserver

类别
导出大小
73 B
@vueuse/rxjs
上次更改
4 个月前

ref 转换为 RxJS Observer 的简易函数。在 @vueuse/rxjs 附加组件中可用。

用法

ts
import { from, fromEvent, toObserver, useSubscription } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import { map, mapTo, startWith, 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).pipe(startWith(0))),
      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,
  startWith,
  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).pipe(startWith(0))),
      map(([curr, total]) => curr + total),
    )
    .subscribe(toObserver(count)),
)

类型声明

typescript
export declare function toObserver<T>(value: Ref<T>): NextObserver<T>

源码

Source文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
Theodros Zelleke
Curt Grimes
yang
Michel Betancourt

更新日志

v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 删除 Vue 2 支持,优化捆绑包并清理 (#4349)

在 MIT 许可证下发布。