Skip to content

useSubject

Category
Export Size
235 B
Package
@vueuse/rxjs
Last Changed
last month

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

Demo

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>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
Curt Grimes
Vincent Schramer
Jiří Peterek

变更日志

No recent changes

Released under the MIT License.