Skip to content

whenever

监视值为真的简写形式。

用法

javascript
import { useAsyncState, whenever } from '@vueuse/core'

const { state, isReady } = useAsyncState(
  fetch('https://jsonplaceholder.typicode.com/todos/1').then(t => t.json()),
  {},
)

whenever(isReady, () => console.log(state))
typescript
import { whenever } from '@vueuse/core'
// ---cut---
// 这个
whenever(ready, () => console.log(state))

// 等同于:
watch(ready, (isReady) => {
  if (isReady)
    console.log(state)
})

回调函数

watch 相同,回调函数将使用 cb(value, oldValue, onInvalidate) 调用。

typescript
import { whenever } from '@vueuse/core'
// ---cut---
whenever(height, (current, lastHeight) => {
  if (current > lastHeight)
    console.log(`高度增加了 ${current - lastHeight}`)
})

计算属性

watch 相同,你可以传递一个获取器函数来在每次更改时计算。

typescript
import { whenever } from '@vueuse/core'
// ---cut---
// 这个
whenever(
  () => counter.value === 7,
  () => console.log('counter 现在为 7!'),
)

选项

选项和默认值与 watch 相同。

typescript
import { whenever } from '@vueuse/core'
// ---cut---
// 这个
whenever(
  () => counter.value === 7,
  () => console.log('counter 现在为 7!'),
  { flush: 'sync' },
)

Type Declarations

ts
type Truthy<T> = T extends false | null | undefined ? never : T
export interface WheneverOptions<
  Immediate = boolean,
> extends WatchOptions<Immediate> {
  /**
   * 当条件满足时仅触发一次
   *
   * 覆盖 `WatchOptions` 中的 `once` 选项
   *
   * @default false
   */
  once?: boolean
}
/**
 * 监视值为真的简写形式
 *
 * @see /whenever
 */
export declare function whenever<T>(
  source: WatchSource<T>,
  cb: WatchCallback<Truthy<T>, T | undefined>,
  options?: WheneverOptions<true>,
): WatchHandle
export declare function whenever<T>(
  source: WatchSource<T>,
  cb: WatchCallback<Truthy<T>, T>,
  options?: WheneverOptions<false>,
): WatchHandle