packages/rxjs/useObservable/index.md
Use an RxJS Observable, return a ref, and automatically unsubscribe from it when the component is unmounted.
import { useObservable } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import { mapTo, scan, startWith } from 'rxjs/operators'
// setup()
const count = useObservable(
interval(1000).pipe(
mapTo(1),
startWith(0),
scan((total, next) => next + total),
),
)
You can provide an initial value that will be used before the Observable emits its first value:
import { useObservable } from '@vueuse/rxjs'
import { interval } from 'rxjs'
const count = useObservable(
interval(1000),
{ initialValue: 0 },
)
// count.value is 0 until the first emission
If you want to add custom error handling to an Observable 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).
import { useObservable } from '@vueuse/rxjs'
import { interval } from 'rxjs'
import { map } from 'rxjs/operators'
// setup()
const count = useObservable(
interval(1000).pipe(
map((n) => {
if (n === 10)
throw new Error('oops')
return n + n
}),
),
{
onError: (err) => {
console.log(err.message) // "oops"
},
},
)
| Option | Type | Description |
|---|---|---|
initialValue | T | Value to use before the Observable emits |
onError | (err: any) => void | Error handler for Observable errors |