inject() in Child Components
inject() with default values, required injection, Symbol keys for namespacing.
inject() in Child Components is a free Vue Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The inject Function
inject retrieves a value an ancestor provided, looked up by the same key. Call it in <script setup>.
import { inject } from "vue";
const theme = inject("theme");Reactivity Is Preserved
If the ancestor provided a ref, the injected value is that same ref. Reading theme.value stays reactive and updates when the provider changes it.
const theme = inject("theme");
// in template: {{ theme }} (auto-unwrapped)Providing a Default Value
If no ancestor provided the key, inject returns undefined. Pass a second argument as a default so the component degrades gracefully.
const theme = inject("theme", "light");
// "light" used when nothing was providedFactory Defaults
For object or expensive defaults, pass a factory function and set the third argument to true, so the default is only created when needed.
const config = inject("config", () => ({ retries: 3 }), true);Injecting Actions
If the provider also offered a function, inject it the same way and call it to request changes - the provider owns the mutation.
const updateName = inject("updateName");
updateName("Grace");Injecting a Store Object
When the provider bundled state and actions, inject the whole object and destructure what you need.
const { user, updateName } = inject("userStore");Type-Safe Injection in TypeScript
Vue exports InjectionKey<T> to make injection type-safe. A typed key tells TypeScript exactly what shape the injected value has.
import type { InjectionKey, Ref } from "vue";
export const themeKey: InjectionKey<Ref<string>> = Symbol("theme");Using the Typed Key
Provide and inject with the typed key. TypeScript now infers the value type and flags mismatches.
provide(themeKey, theme); // theme: Ref<string>
const theme = inject(themeKey); // inferred Ref<string> | undefinedSymbol Keys for Uniqueness
Using a Symbol as the key (often combined with InjectionKey) guarantees no string collisions between unrelated providers in a large codebase.
export const authKey = Symbol("auth") as InjectionKey<AuthService>;Handling Missing Providers
In strict setups you may want to throw if a required injection is missing, surfacing wiring bugs early instead of silently using undefined.
const auth = inject(authKey);
if (!auth) throw new Error("auth provider missing");Inject Anywhere in the Subtree
A component can inject regardless of depth, as long as some ancestor provided the key. Intermediate components stay untouched - the whole point of provide/inject.
Quick Check
What does inject("theme", "light") return when no provider exists?
Recap
You learned inject(key) reads a provided value, preserving reactivity for refs. Pass a default (or factory) for graceful fallback, and use InjectionKey<T> with Symbol keys for type-safe, collision-free injection.
Frequently asked questions
Is the “inject() in Child Components” lesson free?
Yes — the full text of “inject() in Child Components” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “inject() in Child Components”?
inject() with default values, required injection, Symbol keys for namespacing. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “inject() in Child Components” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Prop Drilling Problem
- provide() in Parent Components
- inject() in Child Components
- Provide/Inject for App-Level State