🧬 Vue Provide / Inject – Share Data Across Component Trees (2025 Guide)
🧲 Introduction – What Are Provide and Inject in Vue?
In Vue, provide
and inject
are a dependency injection mechanism that lets parent components share data or methods with deeply nested child components without prop drilling. This approach is ideal for global values like themes, user context, or configuration settings.
🎯 In this guide, you’ll learn:
- How
provide
andinject
work in Vue - Syntax and usage with both Options and Composition API
- Practical real-world examples
- Best practices and limitations
📘 When to Use Provide/Inject
Scenario | Why Use Provide/Inject? |
---|---|
Avoid prop drilling | Share data with deep nested components |
Global service injection | Pass down services like i18n or auth |
Lightweight alternative to Vuex | Simple dependency injection pattern |
Plugin or reusable component dev | Allows controlled config sharing |
🧱 Basic Example – Using Provide and Inject
✅ Parent Component (App.vue
)
<template>
<Child />
</template>
<script>
import Child from './Child.vue';
export default {
components: { Child },
provide() {
return {
themeColor: 'dark'
};
}
};
</script>
✅ Child Component (Child.vue
)
<template>
<div>Theme: {{ themeColor }}</div>
</template>
<script>
export default {
inject: ['themeColor']
};
</script>
🧠 The themeColor
is now available in any nested component that declares inject
.
🧬 Provide/Inject with Composition API
✅ Parent Setup
<script setup>
import { provide } from 'vue';
provide('user', { name: 'Alice', role: 'admin' });
</script>
✅ Child Setup
<script setup>
import { inject } from 'vue';
const user = inject('user');
</script>
<template>
<div>User: {{ user.name }}</div>
</template>
🧠 With inject()
, you can retrieve the shared value reactively.
🧰 Real-World Use Case – Form Validation Context
Parent provides a validation schema:
provide('validation', {
required: value => !!value || 'Field is required'
});
Child inputs use the validation function:
const validation = inject('validation');
const error = validation.required(inputValue);
⚠️ Limitations of Provide/Inject
Limitation | Description |
---|---|
Not reactive unless wrapped | Provide must share reactive refs or objects |
Only works with direct descendants | No support for sibling communication |
Should not replace props/emit | Not meant for standard parent-child interaction |
✅ Making Provide Reactive
<script setup>
import { provide, ref } from 'vue';
const theme = ref('light');
provide('theme', theme);
</script>
In Child:
const theme = inject('theme');
theme.value = 'dark';
🧠 Now updates will be reflected reactively across components.
📌 Summary – Recap & Next Steps
Vue’s provide
and inject
simplify context sharing between deeply nested components without cluttering your props. It’s an efficient way to share configuration, global data, or services.
🔍 Key Takeaways:
provide
sets the data at the parent levelinject
allows any descendant to access it- Works with both Options and Composition API
- Wrap values in
ref
for reactivity - Avoid using it to replace props for standard flows
⚙️ Real-World Relevance:
Ideal for global themes, user sessions, plugin config, or libraries needing scoped context.
❓ FAQ Section
❓ What is the difference between props and inject in Vue?
✅ props
are for explicit parent-child communication. inject
is for context-like access deep in the tree.
❓ Is provide
reactive by default?
✅ No. You must wrap values with ref()
or reactive()
to make them reactive.
❓ Can multiple components inject the same value?
✅ Yes. Any descendant of the provider can access the injected key.
❓ Should I use provide/inject
instead of Vuex?
✅ Only for simple use cases. For complex state, use Vuex or Pinia.
❓ Can inject
access a method or object?
✅ Yes. You can provide objects, methods, or even whole services.
Share Now :