Lzh on GitHub

watch 带有 { once: true } 的简写。回调函数只触发一次后,监听器就会停止。详情请参阅 Vue 官方文档

用法

类似于 watch,但带有 { once: true }

import { watchOnce } from '@vueuse/core'

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

类型声明

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