📖 Scenario: You are building a simple Vue app where a child component needs to use a shared message from its parent using dependency injection.
🎯 Goal: Create a Vue app with a parent component that provides a message using provide and a child component that injects and displays that message using inject.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a parent component that provides a string message with provide
Create a child component that injects the message with inject
Render the child component inside the parent component
Display the injected message inside the child component template
💡 Why This Matters
🌍 Real World
Dependency injection helps share data or services between components without passing props through many layers. This pattern is common in Vue apps for global settings, themes, or shared state.
💼 Career
Understanding dependency injection in Vue is important for building scalable and maintainable applications, a key skill for frontend developer roles.
Progress0 / 4 steps
1
Create the Parent Component with Provided Message
Create a Vue component file named Parent.vue. Inside the <script setup> block, import provide from 'vue'. Then create a constant called message with the value 'Hello from Parent'. Use provide to share this message with the key 'sharedMessage'. In the template, add a <div> with the text Parent Component and include the <Child /> component below it. Import the Child component from './Child.vue'.
Vue
Hint
Remember to import provide from 'vue' and call it inside <script setup>. Also import the Child component and use it in the template.
2
Create the Child Component with Injected Message
Create a Vue component file named Child.vue. Inside the <script setup> block, import inject from 'vue'. Use inject to get the value with the key 'sharedMessage' and store it in a constant called injectedMessage. In the template, display the text Child Component: followed by the injected message using interpolation.
Vue
Hint
Use inject with the same key 'sharedMessage' to receive the message provided by the parent.
3
Use the Parent Component in the App
In your main application file (e.g., App.vue), import the Parent component from './Parent.vue'. Inside the <script setup> block, import Parent. In the template, render the <Parent /> component.
Vue
Hint
Import the Parent component and use it in the template to see the full app with dependency injection.
4
Add Accessibility and Responsive Styling
In Parent.vue, add a role="region" attribute and aria-label="Parent container" to the outer <div>. In Child.vue, add role="region" and aria-label="Child message" to the outer <div>. Also, add a simple CSS style block in both components to center the text and add padding. Use rem units for padding and font size. Ensure the layout looks good on small screens by adding a media query that reduces font size on screens narrower than 400px.
Vue
Hint
Use semantic roles and aria-labels on container divs for accessibility. Use rem units for padding and font size. Add a media query for small screens.
Practice
(1/5)
1. What is the main purpose of using provide and inject in Vue?
easy
A. To define routes in a Vue application
B. To share data or functions between components without passing props manually
C. To handle component lifecycle hooks
D. To create global CSS styles for components
Solution
Step 1: Understand the role of provide/inject
They allow sharing data or functions from a parent component to its descendants without passing props through every intermediate component.
Step 2: Compare with other options
The other options relate to defining routes, creating global CSS styles, and handling component lifecycle hooks, which are unrelated to provide/inject.
Final Answer:
To share data or functions between components without passing props manually -> Option B
Quick Check:
Dependency injection = share data without props [OK]
Hint: Remember provide/inject shares data without prop drilling [OK]
Common Mistakes:
Confusing provide/inject with props or events
Thinking provide/inject manages styles or routing
Assuming provide/inject replaces Vuex or global state
2. Which of the following is the correct syntax to provide a value in a Vue 3 component using the Composition API?
easy
A. const value = inject('key')
B. this.provide('key', value)
C. provide('key', value)
D. inject('key', value)
Solution
Step 1: Identify provide syntax in Composition API
In Vue 3 Composition API, you call provide('key', value) inside setup() to share data.
Step 2: Check other options
const value = inject('key') is for injection, not providing. this.provide('key', value) uses this which is not used in setup(). inject('key', value) is incorrect because inject does not provide values.
Final Answer:
provide('key', value) -> Option C
Quick Check:
Provide = provide('key', value) [OK]
Hint: Provide uses provide('key', value) inside setup() [OK]
Common Mistakes:
Using inject instead of provide to share data
Trying to call provide on this inside setup()
Confusing provide syntax with options API methods
3. Given the following Vue 3 setup, what will be logged in the child component?
A. inject is missing a default value, may cause undefined
B. provide is called outside setup()
C. provide must return an object
D. inject cannot be used in setup()
Solution
Step 1: Check provide usage
Provide is correctly called inside setup() with key 'data' and value 123.
Step 2: Check inject usage
Inject is called correctly but without a default value. If the key is missing, it returns undefined, which might cause issues.
Final Answer:
inject is missing a default value, may cause undefined -> Option A
Quick Check:
Inject without default can be undefined [OK]
Hint: Always provide default value to inject to avoid undefined [OK]
Common Mistakes:
Thinking provide must return an object
Believing inject can't be used in setup
Assuming provide is outside setup here
5. You want to share a reactive object between a grandparent and a grandchild component using provide/inject in Vue 3. Which approach correctly ensures the grandchild reacts to changes in the shared object?
hard
A. Provide a reactive object using provide('state', reactiveObj) and inject it normally in the grandchild
B. Provide a plain object and inject it with a default value in the grandchild
C. Provide a reactive object but inject it inside onMounted hook in the grandchild
D. Provide a reactive object but convert it to a ref before injecting
Solution
Step 1: Understand reactive sharing with provide/inject
Providing a reactive object directly shares its reactivity, so descendants see updates.
Step 2: Evaluate options for reactivity
Provide a reactive object using provide('state', reactiveObj) and inject it normally in the grandchild correctly provides a reactive object and injects it normally, preserving reactivity. Provide a plain object and inject it with a default value in the grandchild uses a plain object, losing reactivity. Provide a reactive object but inject it inside onMounted hook in the grandchild delays injection unnecessarily. Provide a reactive object but convert it to a ref before injecting converts reactive to ref, which is unnecessary and may cause confusion.
Final Answer:
Provide a reactive object using provide('state', reactiveObj) and inject it normally in the grandchild -> Option A
Quick Check:
Reactive object provided and injected keeps reactivity [OK]
Hint: Provide reactive object directly to keep reactivity in inject [OK]
Common Mistakes:
Providing plain objects losing reactivity
Injecting inside lifecycle hooks unnecessarily
Converting reactive to ref before injecting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.