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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.
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.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 42
- Lessons
- 156
Frequently Asked Questions
Is the “inject() in Child Components” lesson free?
Yes — any 3 lessons of the Vue Academy learning path, including “inject() in Child Components”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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