0Pricing
Vue Academy · Lesson

Provide/Inject for App-Level State

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

Provide/Inject for App-Level State is a free Vue Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Provide/Inject for App-Level State” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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