Vue provide and inject let an ancestor component share reactive state directly with deeply nested child components. Instead of forwarding props manually through five intermediate components—a problem called prop drilling—you provide data once at the top and inject it wherever needed.
This pattern works well for UI themes, user locale settings, form group state, or modal controllers tied to a component subtree.
flowchart TD
Root["ThemeParent.vue (provide)"]
Middle["MiddleLayout.vue (no props needed)"]
Child["DeepChild.vue (inject)"]
Root --> Middle --> Child
Root -. Direct Injection .-> Child
provide and inject Work Togetherprovide(key, value): Called inside <script setup> of an ancestor component to expose a ref, reactive object, or function to its descendants.inject(key, defaultValue): Called inside <script setup> of any descendant component to read the provided value, with an optional fallback default.In ThemeParent.vue, provide both the reactive theme ref and the toggleTheme function so child components stay reactive when the theme changes.
<script setup>
import { ref, provide } from 'vue';
import DeepChild from './DeepChild.vue';
const theme = ref('dark');
function toggleTheme() {
theme.value = theme.value === 'dark' ? 'light' : 'dark';
}
provide('themeContext', {
theme,
toggleTheme
});
</script>
<template>
<div :class="`theme-${theme}`" class="parent-box">
<h2>Ancestor Theme Provider</h2>
<button @click="toggleTheme">Toggle from Parent</button>
<DeepChild />
</div>
</template>
In DeepChild.vue, inject 'themeContext' and pass a fallback object in case the component renders outside a provider.
<script setup>
import { inject } from 'vue';
const { theme, toggleTheme } = inject('themeContext', {
theme: 'light',
toggleTheme: () => {}
});
</script>
<template>
<div class="nested-child">
<h3>Deeply Nested Child</h3>
<p>Active Theme: <strong>{{ theme }}</strong></p>
<button @click="toggleTheme">Toggle from Child</button>
</div>
</template>
provide('theme', theme.value) passes a plain string snapshot, breaking reactivity. Always pass the ref itself (provide('theme', theme)).provide() or inject() Inside Async Callbacks: Both functions must run synchronously during <script setup> initialization. Calling inject() inside setTimeout or after await fails to find the active component instance.Yes. Call app.provide('appConfig', config) in src/main.js before app.mount('#app') so every component in the app can inject it.
inject() finds no matching provide()?Vue logs a warning and returns undefined, unless you pass a second default argument like inject('theme', 'light').
provide/inject or Pinia for state management?Use provide/inject for component-subtree state like tabs, theme wrappers, or form contexts. Use Pinia for global application state shared across unrelated routes.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
You've completed this section! Take a quick 5-question quiz to check your understanding.