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>[]>>(
  source: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, true>>,
  options?: Omit<WatchOptions<true>, "once">,
): WatchStopHandle
export declare function watchOnce<T>(
  source: WatchSource<T>,
  cb: WatchCallback<T, T | undefined>,
  options?: Omit<WatchOptions<true>, "once">,
): WatchStopHandle
export declare function watchOnce<T extends object>(
  source: T,
  cb: WatchCallback<T, T | undefined>,
  options?: Omit<WatchOptions<true>, "once">,
): WatchStopHandle

源代码

源代码文档

变更日志

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