Vue.js & Composition API (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
Dependency Injection with `provide()` and `inject()`.
senior LevelMost AskedCode ExampleDependency InjectionSystem Design
In Simple Words: Passes state from an ancestor component down to deeply nested children without prop drilling:
typescript
// Ancestor.vue
import { provide, ref } from 'vue';
const theme = ref('dark');
provide('theme-key', theme);
// DeepChild.vue
import { inject } from 'vue';
const theme = inject('theme-key', 'light'); // with fallback