Skip to content

useSubject

类别
导出体积
235 B
依赖包
@vueuse/rxjs
上次更改
2 weeks ago

将 RxJS Subject 绑定到一个 ref 上,并在两者之间传播值变化。

示例

Available in the @vueuse/rxjs add-on.

用法

ts
import { useSubject } from '@vueuse/rxjs'
import { Subject } from 'rxjs'

const subject = new Subject()

// setup()
const subjectRef = useSubject(subject)

如果你想要为可能出错的 Subject 添加自定义错误处理,你可以提供一个可选的 onError 配置。如果没有提供,RxJS 将把提供的 observable 中的任何错误视为 “未处理的错误”,并且它将在一个新的调用栈中抛出,并报告给 window.onerror (或者如果你恰好在 Node 中,则为 process.on('error'))。

ts
import { useSubject } from '@vueuse/rxjs'
import { Subject } from 'rxjs'

const subject = new Subject()

// setup()
const subjectRef = useSubject(subject, {
  onError: (err) => {
    console.log(err.message) // "oops"
  },
})

类型声明

typescript
export interface UseSubjectOptions<I = undefined>
  extends Omit<UseObservableOptions<I>, "initialValue"> {}
export declare function useSubject<H>(
  subject: BehaviorSubject<H>,
  options?: UseSubjectOptions,
): Ref<H>
export declare function useSubject<H>(
  subject: Subject<H>,
  options?: UseSubjectOptions,
): Ref<H | undefined>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
Curt Grimes
Vincent Schramer
Jiří Peterek

更新日志

没有最近的更新日志