Skip to content

watchAtMost

类别
导出体积
510 B
上次更改
2 weeks ago

watch 监听器带有触发次数限制。

使用方法

类似于 watch,但多了一个选项 count,用于设置回调函数触发的次数。达到次数后,监听将自动停止。

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

watchAtMost(
  source,
  () => { console.log('触发!') }, // 最多触发 3 次
  {
    count: 3, // 触发次数
  },
)

类型声明

typescript
export interface WatchAtMostOptions<Immediate>
  extends WatchWithFilterOptions<Immediate> {
  count: MaybeRefOrGetter<number>
}
export interface WatchAtMostReturn {
  stop: WatchStopHandle
  count: Ref<number>
}
export declare function watchAtMost<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false,
>(
  sources: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options: WatchAtMostOptions<Immediate>,
): WatchAtMostReturn
export declare function watchAtMost<
  T,
  Immediate extends Readonly<boolean> = false,
>(
  sources: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options: WatchAtMostOptions<Immediate>,
): WatchAtMostReturn

源码

源码文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
vaakian X
lvjiaxuan
lvjiaxuan
HG
webfansplz

更新日志

v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue