providedIn: "root" đăng ký service ở root injector, thường là singleton toàn app và tree-shakable nếu không dùng.
Ví dụ component provider tạo store riêng cho mỗi wizard:
@Component({
selector: "app-wizard",
providers: [WizardStore],
template: "<ng-content />",
})
export class WizardComponent {}Provider trong component tạo instance ở injector của component đó và áp dụng cho subtree bên dưới.
Dùng root cho shared stateless/service toàn app; dùng component provider khi cần state riêng theo mỗi instance component, ví dụ wizard, tab hoặc embedded feature.
providedIn: "root" registers a service in the root injector, usually as an app-wide singleton and tree-shakable when unused.
Component provider example creating a separate store per wizard:
@Component({
selector: "app-wizard",
providers: [WizardStore],
template: "<ng-content />",
})
export class WizardComponent {}A component provider creates an instance in that component injector and applies to the subtree below it.
Use root for app-wide shared/stateless services; use component providers when each component instance needs its own state, such as a wizard, tab or embedded feature.