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/computedInject.md
1---2category: Component3---45# computedInject67Combine `computed` and `inject`. Useful for creating a computed property based on an injected value.89## Usage1011In Provider Component1213```ts twoslash include main14import type { InjectionKey, Ref } from 'vue'15import { provide, ref } from 'vue'1617interface Item {18key: number19value: string20}2122export const ArrayKey: InjectionKey<Ref<Item[]>> = Symbol('symbol-key')2324const array = ref([{ key: 1, value: '1' }, { key: 2, value: '2' }, { key: 3, value: '3' }])2526provide(ArrayKey, array)27```2829In Receiver Component3031```ts32// @filename: provider.ts33// @include: main34// ---cut---35import { computedInject } from '@vueuse/core'3637import { ArrayKey } from './provider'3839const computedArray = computedInject(ArrayKey, (source) => {40const arr = [...source.value]41arr.unshift({ key: 0, value: 'all' })42return arr43})44```4546### Default Value4748You can provide a default value that will be used if the injection key is not provided by a parent component.4950```ts51import { computedInject } from '@vueuse/core'5253const computedArray = computedInject(54ArrayKey,55(source) => {56return source.value.map(item => item.value)57},58ref([]), // default source value59)60```6162### Factory Default6364Pass `true` as the fourth argument to treat the default value as a factory function.6566```ts67import { computedInject } from '@vueuse/core'6869const computedArray = computedInject(70ArrayKey,71(source) => {72return source.value.map(item => item.value)73},74() => ref([]), // factory function for default75true, // treat default as factory76)77```7879### Writable Computed8081You can also create a writable computed property by passing an object with `get` and `set` functions.8283```ts84import { computedInject } from '@vueuse/core'8586const computedArray = computedInject(ArrayKey, {87get(source) {88return source.value.map(item => item.value)89},90set(value) {91// handle setting the value92console.log('Setting value:', value)93},94})95```9697## Type Declarations9899```ts100export type ComputedInjectGetter<T, K> = (101source: T | undefined,102oldValue?: K,103) => K104export type ComputedInjectGetterWithDefault<T, K> = (105source: T,106oldValue?: K,107) => K108export type ComputedInjectSetter<T> = (v: T) => void109export interface WritableComputedInjectOptions<T, K> {110get: ComputedInjectGetter<T, K>111set: ComputedInjectSetter<K>112}113export interface WritableComputedInjectOptionsWithDefault<T, K> {114get: ComputedInjectGetterWithDefault<T, K>115set: ComputedInjectSetter<K>116}117export declare function computedInject<T, K = any>(118key: InjectionKey<T> | string,119getter: ComputedInjectGetter<T, K>,120): ComputedRef<K | undefined>121export declare function computedInject<T, K = any>(122key: InjectionKey<T> | string,123options: WritableComputedInjectOptions<T, K>,124): ComputedRef<K | undefined>125export declare function computedInject<T, K = any>(126key: InjectionKey<T> | string,127getter: ComputedInjectGetterWithDefault<T, K>,128defaultSource: T,129treatDefaultAsFactory?: false,130): ComputedRef<K>131export declare function computedInject<T, K = any>(132key: InjectionKey<T> | string,133options: WritableComputedInjectOptionsWithDefault<T, K>,134defaultSource: T | (() => T),135treatDefaultAsFactory: true,136): ComputedRef<K>137```138