Skip to content

Commit 31c069a

Browse files
author
ying.liu
committed
add di
1 parent 6f8bfb6 commit 31c069a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

frontend/angular-best-practices.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ createForm() {
2626
```
2727

2828
For most simple scenarios, use `FormBuilder` to create a form. For a complicated form, create `FormGroup` and `FormControl` directly.
29+
30+
## Dependency Injection
31+
32+
- Use `providedIn: 'root'` for services which should be available in whole application as singletons.
33+
- Never use `providedIn: EagerlyImportedModule`, you don’t need it and if there is some super exceptional use case then go with the `providers: []` instead.
34+
- Use `providedIn: LazyServiceModule` to prevent service injection in the eagerly imported part of the application. Use `LazyServiceModule` which will be imported by `LazyModule` to prevent circular dependency warning. LazyModule will then be lazy loaded using Angular Router for some route in a standard fashion. This is not recommended for too much confusing code.
35+
- Use `providers: []` inside of `@Component` or `@Directive` to scope service only for the particular component sub-tree which will also lead to creation of multiple service instances (one service instance per one component usage)
36+
- Always try to scope your services conservatively to prevent dependency creep and resulting tangled hell

0 commit comments

Comments
 (0)