What you will learn
Move shared domain logic behind a typed injectable service.
- Dependency injection supplies a dependency instead of making every consumer construct it. inject() is convenient in field initializers and other injection contexts.
- providedIn: 'root' creates an application-wide provider that can be tree-shaken when unused. Component providers deliberately create a nearer instance.
- A service can own shared signals and business operations. Components should still own state that is only meaningful to their local UI.
- Program to a small public service API. Do not let components reach into mutable collections and change them directly.
How it works
1Injector owns provider
2Component injects service
3Service updates domain state
4Consumers react
Expected result
Add creates a shared todo through the service; Delete removes it and Remaining updates.
Common mistakes
Do not turn every helper into a root service. Do not expose writable shared signals when consumers only need read access. Avoid hidden service-to-service cycles.
