fn
watchOnce
v0.0.15testeddemoShorthand for `watch` that automatically stops after the callback fires once.
Examples
ts
const count = ref(0);
watchOnce(count, value => console.log('fired once with', value));ts
watchOnce([a, b], ([a, b]) => console.log(a, b));Demo
Loading demo…
Signatures
ts
export function watchOnce<T>(
source: WatchSource<T>,
cb: WatchCallback<T, T | undefined>,
options?: WatchOnceOptions<true>,
): WatchHandle;ts
export function watchOnce<T extends Readonly<MultiWatchSources>>(
source: [...T],
cb: WatchCallback<MapSources<T>, MapOldSources<T, true>>,
options?: WatchOnceOptions<true>,
): WatchHandle;ts
export function watchOnce<T extends object>(
source: T,
cb: WatchCallback<T, T | undefined>,
options?: WatchOnceOptions<true>,
): WatchHandle;Parameters
| Parameter | Type | Description |
|---|---|---|
source | object | WatchSource<unknown> | MultiWatchSources | The reactive source (ref, getter, reactive object, or array of sources) to watch |
cb | WatchCallback | Invoked once with the new value, old value, and `onCleanup` |
options? | WatchOnceOptions<boolean> | undefined | Watch options (`immediate`, `deep`, `flush`); `once` is forced on |
Returns
WatchHandleA handle to stop watching before the first trigger