Skip to content

Service-based State

Services with BehaviorSubject provide a simple yet powerful state management solution for mid-sized applications.

@Injectable({ providedIn: 'root' })
export class UserStateService {
private currentUser = new BehaviorSubject<User | null>(null);
readonly user$ = this.currentUser.asObservable();
setUser(user: User) { this.currentUser.next(user); }
clearUser() { this.currentUser.next(null); }
get currentUserValue() { return this.currentUser.value; }
}
  • Shared state across a feature
  • Simple state without complex side effects

Service-based state balances simplicity with functionality for most applications.”