Sub topic 12
Letting features talk without coupling them
This chapter assumes you know the earlier chapters' feature folders, route-scoped providers, barrels, and the layer names (feature, ui, data-access, util). It also assumes basic RxJS and signals.
The problem
Features sometimes need each other. Placing an order should update a customer's activity list. The easy fix is to import OrdersStore into the customers feature. That creates three problems:
- Customers can no longer be built or tested without orders.
- The import pulls orders code into the customers lazy chunk.
- It bypasses the barrel, so the lint rules from the last chapter will flag it.
The goal is for features to depend on a small shared contract, not on each other.
Three options
| Option | Use it when | Coupling |
|---|---|---|
| URL and route params | One feature links to another's page | Only the URL shape |
| Parent composes children with inputs and outputs | Two features sit on one page | Parent knows both |
| Shared event or state service | Features react to changes elsewhere, with no parent in common | Only the shared contract |
Try them in this order. The URL is the cheapest, and a shared service is the heaviest.
Option 1: navigate by URL
A link such as /customers/42 needs no import. The orders feature only needs to know the path. If you want to avoid hard-coded paths, export a small path helper from the customers barrel.
Option 3: a shared event contract
Put the contract in a layer that both features may depend on, such as a shared data-access or util library. It holds the event type and a service. It holds no business logic.
// src/app/shared/events/order-events.ts
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
export interface OrderPlaced {
orderId: string;
customerId: string;
}
@Injectable({ providedIn: 'root' })
export class OrderEvents {
private readonly placedSubject = new Subject<OrderPlaced>();
readonly placed$ = this.placedSubject.asObservable();
publishPlaced(event: OrderPlaced): void {
this.placedSubject.next(event);
}
}The orders feature publishes:
// src/app/orders/data-access/order-placer.ts
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { OrderEvents } from '@app/shared/events/order-events';
@Injectable()
export class OrderPlacer {
private readonly http = inject(HttpClient);
private readonly events = inject(OrderEvents);
place(customerId: string): void {
this.http
.post<{ id: string }>('/api/orders', { customerId })
.subscribe(({ id }) => this.events.publishPlaced({ orderId: id, customerId }));
}
}The customers feature subscribes and turns the event into its own signal state:
// src/app/customers/data-access/customer-activity.store.ts
import { Injectable, inject, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { OrderEvents } from '@app/shared/events/order-events';
@Injectable()
export class CustomerActivityStore {
private readonly _recentOrderIds = signal<string[]>([]);
readonly recentOrderIds = this._recentOrderIds.asReadonly();
constructor() {
inject(OrderEvents)
.placed$.pipe(takeUntilDestroyed())
.subscribe((e) => this._recentOrderIds.update((ids) => [...ids, e.orderId]));
}
}Neither feature imports the other. Each one only knows OrderEvents. The @app/shared/* alias comes from the path alias setup in an earlier chapter.
Common errors
- Shared services that grow. If
shared/eventscollects business rules, you have rebuilt a hidden feature. Keep it to types and thin plumbing. - Events for everything. A chain of events is hard to trace. Prefer inputs and outputs when a parent already owns both children.
- Forgetting to unsubscribe. Use
takeUntilDestroyed()in an injection context, as shown above. - Sharing mutable state. Expose read-only signals with
asReadonly(), as the feature store does.
Exercise
The customers feature needs to show the name of the logged-in user, which the auth feature owns. Which option from the table fits, and where should the code live?
Show answer
Use a shared state service. Put a small auth store with a read-only user signal in a shared data-access layer, or export it through auth's barrel if lint rules allow it. Customers reads the signal. This is current state rather than a one-off event, so a non-replaying Subject would be the wrong tool.