0
0
Vueframework~20 mins

Dependency injection patterns in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dependency Injection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue 3 component output when using provide/inject?

Consider this Vue 3 setup using provide and inject. What will the child component display?

Vue
/* Parent.vue */
<script setup>
import { provide } from 'vue'
const message = 'Hello from parent'
provide('msg', message)
</script>
<template>
  <Child />
</template>

/* Child.vue */
<script setup>
import { inject } from 'vue'
const msg = inject('msg', 'Default message')
</script>
<template>
  <p>{{ msg }}</p>
</template>
ADisplays: Default message
BDisplays: Hello from parent
CThrows an error because inject key is missing
DDisplays nothing because provide is not reactive
Attempts:
2 left
💡 Hint

Remember that provide passes data down the component tree and inject receives it.

📝 Syntax
intermediate
1:30remaining
Which option correctly injects a provided value in Vue 3 Composition API?

Choose the correct syntax to inject a provided value named config in a Vue 3 component using inject.

Aconst config = inject('config', defaultValue)
Bconst config = inject('config')
Cconst config = inject(config)
Dconst config = provide('config')
Attempts:
2 left
💡 Hint

Inject accepts a key string and optionally a default value.

🔧 Debug
advanced
2:30remaining
Why isn't the injected value reactive in this Vue 3 component?

Given this parent and child component, why is the injected value in the child not reactive?

Vue
/* Parent.vue */
<script setup>
import { provide, ref } from 'vue'
const count = ref(5)
provide('count', count.value)
</script>
<template>
  <Child />
</template>

/* Child.vue */
<script setup>
import { inject } from 'vue'
const count = inject('count', 0)
</script>
<template>
  <p>{{ count }}</p>
</template>
ABecause inject key 'count' is misspelled in child
BBecause provide must be called inside setup(), not at top level
CBecause provide passes a primitive value, the child gets a static copy, not reactive
DBecause ref values cannot be provided or injected
Attempts:
2 left
💡 Hint

Think about reactivity and how ref values are passed.

🧠 Conceptual
advanced
1:30remaining
What is a key benefit of using dependency injection in Vue 3?

Why do developers use dependency injection patterns like provide and inject in Vue 3?

ATo replace Vuex or Pinia as the only state management solution
BTo tightly couple components so they depend on each other directly
CTo automatically update the DOM without using reactive data
DTo share data or services across components without prop drilling
Attempts:
2 left
💡 Hint

Think about how data flows between components.

state_output
expert
3:00remaining
What will be the output after clicking the button in this Vue 3 example using provide/inject with reactive state?

Analyze this Vue 3 code. What will the child component display after the button is clicked once?

Vue
/* Parent.vue */
<script setup>
import { ref, provide } from 'vue'
const count = ref(0)
provide('count', count)
function increment() {
  count.value++
}
</script>
<template>
  <button @click="increment">Increment</button>
  <Child />
</template>

/* Child.vue */
<script setup>
import { inject } from 'vue'
const count = inject('count')
</script>
<template>
  <p>Count is: {{ count.value }}</p>
</template>
ACount is: 1
BCount is: 0
CCount is: undefined
DCount is: NaN
Attempts:
2 left
💡 Hint

Remember that ref is reactive and shared via provide/inject.