Skip to content

useSubject

将 RxJS Subject 绑定到 ref 并以双向方式传播值更改。

¥Bind an RxJS Subject to a ref and propagate value changes both ways.

示例

Available in the @vueuse/rxjs add-on.

用法

¥Usage

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

const subject = new Subject()

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

如果你想向可能出错的主题添加自定义错误处理,你可以提供可选的 onError 配置。如果没有这个,RxJS 会将所提供的可观察量中的任何错误视为 "未处理的错误",并将其抛出到新的调用堆栈中并报告给 window.onerror(如果你碰巧在节点中,则报告给 process.on('error'))。

¥If you want to add custom error handling to a Subject that might error, you can supply an optional onError configuration. Without this, RxJS will treat any error in the supplied observable as an "unhandled error" and it will be thrown in a new call stack and reported to window.onerror (or process.on('error') if you happen to be in node).

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>

源代码

源代码示例文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号