📖 Scenario: You are building a Vue app with nested components. You want to share a message from the top-level component to a deeply nested child without passing props through every level.
🎯 Goal: Build a Vue app that uses provide in the top component and inject in a deeply nested child component to share a message.
📋 What You'll Learn
Create a top-level component that provides a message using provide
Create a nested child component inside the top component
Create a deeply nested child component inside the nested child
Use inject in the deeply nested child to receive the message
Display the injected message in the deeply nested child component
💡 Why This Matters
🌍 Real World
In real apps, provide/inject helps share data like themes, user info, or settings across many nested components without passing props at every level.
💼 Career
Understanding provide/inject is important for Vue developers to write clean, maintainable code and manage state efficiently in component trees.
Progress0 / 4 steps
1
Set up the top-level component with a message
Create a Vue component called App with a setup function that defines a constant message with the value 'Hello from top!'.
Vue
Hint
Use const message = 'Hello from top!' inside the setup function.
2
Provide the message from the top-level component
In the setup function of App, use Vue's provide function to provide the message with the key 'message'.
Vue
Hint
Import provide from 'vue' and call provide('message', message) inside setup.
3
Create nested and deeply nested child components
Create two components: NestedChild and DeepNestedChild. Inside NestedChild, include <DeepNestedChild /> in the template. Register both components in App and use <NestedChild /> inside App's template.
Vue
Hint
Define DeepNestedChild and NestedChild components with templates. Nest DeepNestedChild inside NestedChild. Use <NestedChild /> in App's template.
4
Inject the message in the deeply nested child and display it
In DeepNestedChild, use Vue's inject function to get the message provided by App. Display the injected message inside the template.
Vue
Hint
Import inject from 'vue'. Use const message = inject('message') inside DeepNestedChild's setup. Show {{ message }} in the template.
Practice
(1/5)
1. What is the main purpose of provide and inject in Vue?
easy
A. To replace Vuex for state management in all cases.
B. To share data deeply between ancestor and descendant components without passing props.
C. To handle component lifecycle hooks automatically.
D. To style components globally using CSS variables.
Solution
Step 1: Understand the role of provide and inject
Provide and inject allow passing data from an ancestor component to any descendant without manually passing props through each intermediate component.
Step 2: Compare with other options
Vuex is a separate state management tool, lifecycle hooks handle component events, and CSS variables style components; none of these are the main purpose of provide/inject.
Final Answer:
To share data deeply between ancestor and descendant components without passing props. -> Option B
2. Which of the following is the correct way to use provide in a Vue 3 setup function?
easy
A. provide('key', 'value')
B. provide: { key: 'value' }
C. this.provide = { key: 'value' }
D. const provide = { key: 'value' }
Solution
Step 1: Recall provide usage in setup()
In Vue 3 setup, provide is a function imported from 'vue' and called as provide('key', value).
Step 2: Check other options
const provide = { key: 'value' } is an object, not a function call; this.provide = { key: 'value' } uses this which is not available in setup; provide: { key: 'value' } is invalid syntax.
Final Answer:
provide('key', 'value') -> Option A
Quick Check:
Provide is a function call in setup [OK]
Hint: Use provide('key', value) inside setup() [OK]
Common Mistakes:
Trying to assign provide as an object
Using this inside setup()
Confusing provide option with function call
3. Given this Vue 3 code snippet, what will be logged in the child component?
A. The key 'theme' is not provided properly because it is misspelled.
B. inject is missing a default value.
C. provide is incorrectly assigned as an object instead of called as a function.
D. setup() cannot return reactive data.
Solution
Step 1: Check provide usage
Provide must be called as a function: provide('key', value). Here, provide is assigned an object, which is invalid.
Step 2: Verify other options
Inject default value is optional; key spelling matches; setup can return reactive data.
Final Answer:
provide is incorrectly assigned as an object instead of called as a function. -> Option C
Quick Check:
Provide must be called, not assigned [OK]
Hint: Provide is a function call, not an object assignment [OK]
Common Mistakes:
Assigning provide as an object instead of calling it
Thinking inject requires default value always
Confusing setup return rules
5. You want to share a reactive theme object deeply across many nested components using provide/inject. Which approach correctly ensures reactivity is preserved in descendants?
hard
A. Provide a reactive object but inject a copy using { ...injected } to avoid mutation.
B. Provide a plain object and inject it, then wrap it with reactive() in each child.
C. Provide a ref with the reactive object and inject the ref, then use .value in children.
D. Provide a reactive object using reactive() and inject it directly.
Solution
Step 1: Understand reactivity with provide/inject
Provide a reactive object created with reactive() and inject it directly. Vue automatically ensures the injected value is deeply reactive in descendants.
Step 2: Analyze other options
Provide a plain object and inject it, then wrap it with reactive() in each child. uses a plain object (no reactivity) and re-wrapping creates separate instances. Provide a ref with the reactive object and inject the ref, then use .value in children. unnecessarily wraps a reactive object in a ref, adding extra complexity with .value. Provide a reactive object but inject a copy using { ...injected } to avoid mutation. copies the object, breaking the reactivity connection.
Final Answer:
Provide a reactive object using reactive() and inject it directly. -> Option D
Quick Check:
reactive() object + direct provide/inject = preserved reactivity [OK]
Hint: Provide reactive({}) directly and inject without .value [OK]
Common Mistakes:
Injecting plain objects and expecting reactivity
Wrapping injected objects again with reactive
Copying objects and losing reactivity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.