Skip to content

watchOnce

使用 { once: true } 观察值的简写。回调触发一次后,监视器将停止。有关完整详细信息,请参阅 Vue 文档

¥Shorthand for watching value with { once: true }. Once the callback fires once, the watcher will be stopped. See Vue's docs for full details.

用法

¥Usage

watch 类似,但与 { once: true } 相同

¥Similar to watch, but with { once: true }

ts
import { watchOnce } from '@vueuse/core'

watchOnce(source, () => {
  // triggers only once
  console.log('source changed!')
})

类型声明

typescript
export declare function watchOnce<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false,
>(
  source: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchOptions<Immediate>,
): WatchStopHandle
export declare function watchOnce<
  T,
  Immediate extends Readonly<boolean> = false,
>(
  sources: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchOptions<Immediate>,
): WatchStopHandle

源代码

源代码文档

变更日志

08f7d - feat: use vue's native once behaviour (#4750)