Vue Academy · Lesson

Provide/Inject for App-Level State

app.provide() for global state, theming, i18n, and avoiding Pinia for simple cases.

Lesson 4 of 413 steps

Provide/Inject for App-Level State is a free Vue Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.

Application-Level provide

Besides component-level provide, you can provide on the app instance with app.provide. The value becomes available to every component in the app.

const app = createApp(App);
app.provide("appVersion", "1.0.0");

Providing Global Services

App-level provide is perfect for singletons like an auth service, an HTTP client, or a logger - things the whole app may need.

import { authService } from "./auth";
app.provide("auth", authService);

Consuming a Global Service

Any component injects the service by key, anywhere in the tree, with no prop drilling and no extra wiring.

const auth = inject("auth");
auth.login(credentials);

Symbol Keys for Globals

For app-wide services, exported Symbol keys (ideally typed) keep injection safe and discoverable across the codebase.

// keys.ts
export const authKey: InjectionKey<AuthService> = Symbol("auth");
// main.ts
app.provide(authKey, authService);

Order of Lookup

When both an app-level and a component-level provide use the same key, the closest provider wins for descendants of that component. App-level acts as the global fallback.

Read-Heavy Scenarios

Provide/inject excels when many components only read shared data - theme, locale, config. Consumers do not need to write, so the pattern stays simple and predictable.

Comparing to Pinia

Pinia is Vue official state-management library. It offers devtools, modular stores, actions, getters, and time-travel debugging - ideal for complex, frequently-mutated global state.

import { defineStore } from "pinia";
export const useAuth = defineStore("auth", { /* ... */ });

When provide/inject Is Enough

For mostly read-only shared values, provide/inject avoids the overhead of a store. It is lighter, built-in, and clear for theme, i18n, or injected services.

When to Reach for Pinia

Choose Pinia when state is mutated from many places, needs devtools, must persist, or has complex derived data. It centralizes logic better than scattered provide/inject pairs.

Combining Both

They are not mutually exclusive. You might use Pinia for app data and provide/inject for lightweight component-tree context like a form or a UI theme.

provide("formContext", { register, errors });

Testing Considerations

When unit testing a component that injects, supply the value through the test mount options global.provide so the component receives a stub.

mount(MyComponent, {
  global: { provide: { auth: fakeAuth } }
});

Quick Check

When is provide/inject preferred over Pinia?

Recap

You learned app.provide exposes global services to every component, consumed by key anywhere via inject. Provide/inject shines for read-heavy shared context, while Pinia suits complex, frequently-mutated global state with devtools support.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “Provide/Inject for App-Level State” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “Provide/Inject for App-Level State”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.

What will I learn in “Provide/Inject for App-Level State”?

app.provide() for global state, theming, i18n, and avoiding Pinia for simple cases. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Provide/Inject for App-Level State” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Prop Drilling Problem
  2. provide() in Parent Components
  3. inject() in Child Components
  4. Provide/Inject for App-Level State
← Back to Vue Academy