الانتقال إلى المحتوى
FrontendAngular· 6 دقائق للقراءة

Sharing State Without a Store Library: Signal-Based Services

A root service is already shared by every component. Hold the state in a private signal, expose it read-only, and change it only through methods.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

The notifications panel has a "Mark as read" button. Click it, and the notification turns grey in the panel, but the bell in the header still says 3. The ticket that comes out of the bug triage reads "Introduce NgRx so components share state correctly", estimated at three days. Yet the header and the panel already inject the same NotificationStore and read the same signal. The bug is one line: markRead() sets item.read = true on an object inside the signal's array, and the signal never finds out.

The common misconception

The belief: Angular components can't really share state on their own. Once two components that aren't parent and child need the same data, you need a store library with actions, reducers and selectors, or the data will drift apart.

It comes from real experience. For years, a store library was the standard answer for shared state in many Angular codebases, and the alternative people saw was often messy: a service with a plain public field that every component mutates directly, and views that sometimes forget to refresh. That chaos is real, and a store library does fix it by putting every change through one path. But the fix doesn't come from the library itself. It comes from three rules the library enforces, and a signal-based service can follow the same three rules in one file.

What actually happens

A root service is already shared

A class decorated with @Service(), Angular's shorthand for @Injectable({ providedIn: 'root' }), is provided in the root injector. The services guide lists what that guarantees: a single, shared instance for the whole application, available anywhere without registering a provider. Every component that calls inject(NotificationStore) gets the same object. Sharing was never the hard part.

Angular has to see the change

The hard part is noticing changes. Since Angular 21, apps run without zone.js by default, and since Angular 22 components default to . The zoneless guide lists what makes Angular run change detection: things like markForCheck(), a template event listener, or an update to a signal that a template reads. Assigning a plain field on a service is none of those, so the header has no reason to check itself again. Put the value in a instead, and every template or computed() that reads it is notified when it changes.

A signal only knows about new values

That's where the bug in the hook comes from. By default, a signal compares the old and new value with Object.is(), so it notices a new value, not a changed object. Mutating an item inside the array keeps the same array, so nothing is notified, and a computed() like the unread count keeps its cached result. The panel happened to re-render, because the click event checked it, which made the bug look like a sharing problem.

The signals guide adds a second warning: asReadonly() stops callers from calling set() or update(), but it has no mechanism to prevent deep mutation of the value. So the rule has to be explicit: every change creates a new array or object.

TypeScript notification-store.ts
import { computed, Service, signal } from '@angular/core';@Service()export class NotificationStore {  // Writable by every component that injects the store  readonly items = signal<AppNotification[]>([]);  readonly unread = computed(    () => this.items().filter((n) => !n.read).length,  );  markRead(id: string) {    const item = this.items().find((n) => n.id === id);    // Mutates in place: same array, so unread() never recomputes    if (item) item.read = true;  }}

The header reads store.unread(), the panel reads store.items(), and both call store.markRead(id). There's one place where notifications change, and every change produces a new array that the signal can see.

Three rules make a store

Look at what the right version does, because those are the rules a store library would enforce:

  1. One source of truth. The state lives in one private signal, the , and everything else is derived from it with computed().
  2. Read-only from outside. Components get asReadonly() signals and computed values, so they can't write past your methods.
  3. Changes through named methods. add() and markRead() are the only ways in, and each replaces the value instead of mutating it.

Scope it where it belongs

Not all shared state is app-wide. A wizard, an editor or a dashboard widget often needs state shared by a component and its children, but separate for each instance. Provide the store in that component's providers instead: the hierarchical injectors guide notes that each component instance then gets a private instance of the service, which is destroyed with the component. Use @Service({ autoProvided: false }) for a store that should only exist where you provide it.

MythReality
Sharing state needs a store libraryA root service is already one shared instance
A field on a service is shared stateIt's shared, but Angular isn't told when it changes
asReadonly() makes the state immutableIt blocks set() and update(), not deep mutation
Mutating an item updates every viewSame reference, so the signal notifies no one
Store libraries are never worth itTheir conventions and tooling can pay off at scale

How to apply it in your project

  1. Find shared state in plain fieldsLook for root services with public mutable fields or arrays. Each one is a view that may not refresh under OnPush and zoneless change detection.
  2. Move it into a private signalReplace each field with a private signal(), expose it with asReadonly(), and turn derived values such as counts and totals into computed().
  3. Route every change through a methodSearch for writes from components, then give each kind of change a method that calls update() and returns a new array or object.
  4. Decide when a library earns its placeIf many teams change the same state through complex flows and need conventions or debugging tools to keep it manageable, evaluate a store library for that state. Keep the rest in signal services.

Key takeaways

  • A root-provided service is already a single instance that every component shares.
  • Hold shared state in a private signal, expose it with asReadonly() and computed(), and change it through methods.
  • Signals notice new values, not mutations, and asReadonly() doesn't stop deep mutation, so always replace the value.
  • Provide a store in a component's providers when each instance needs its own copy.

📚 المصادر

  1. Converting writable signals to readonly — Angular Signals
  2. Creating and using services — Angular
  3. Hierarchical injectors — Angular
  4. Angular without ZoneJS (Zoneless) — Angular

التعليقات (0)