Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/computedWithControl.md
1---2category: Reactivity3alias: controlledComputed4---56# computedWithControl78Explicitly define the dependencies of computed.910## Usage1112```ts twoslash include main13import { computedWithControl } from '@vueuse/core'1415const source = ref('foo')16const counter = ref(0)1718const computedRef = computedWithControl(19() => source.value, // watch source, same as `watch`20() => counter.value, // computed getter, same as `computed`21)22```2324With this, the changes of `counter` won't trigger `computedRef` to update but the `source` ref does.2526```ts27// @include: main28// ---cut---29console.log(computedRef.value) // 03031counter.value += 13233console.log(computedRef.value) // 03435source.value = 'bar'3637console.log(computedRef.value) // 138```3940### Manual Triggering4142You can also manually trigger the update of the computed by:4344```ts45// @include: main46// ---cut---47const computedRef = computedWithControl(48() => source.value,49() => counter.value,50)5152computedRef.trigger()53```5455### Deep Watch5657Unlike `computed`, `computedWithControl` is shallow by default.58You can specify the same options as `watch` to control the behavior:5960```ts61const source = ref({ name: 'foo' })6263const computedRef = computedWithControl(64source,65() => counter.value,66{ deep: true },67)68```6970## Type Declarations7172```ts73export interface ComputedWithControlRefExtra {74/**75* Force update the computed value.76*/77trigger: () => void78}79export interface ComputedRefWithControl<T>80extends ComputedRef<T>, ComputedWithControlRefExtra {}81export interface WritableComputedRefWithControl<T>82extends WritableComputedRef<T>, ComputedWithControlRefExtra {}83export type ComputedWithControlRef<T = any> =84| ComputedRefWithControl<T>85| WritableComputedRefWithControl<T>86export declare function computedWithControl<T>(87source: WatchSource | MultiWatchSources,88fn: ComputedGetter<T>,89options?: WatchOptions,90): ComputedRefWithControl<T>91export declare function computedWithControl<T>(92source: WatchSource | MultiWatchSources,93fn: WritableComputedOptions<T>,94options?: WatchOptions,95): WritableComputedRefWithControl<T>96/** @deprecated use `computedWithControl` instead */97export declare const controlledComputed: typeof computedWithControl98```99