Skip to content

Commit 954d7a7

Browse files
ying.liuying.liu
authored andcommitted
revise module types
1 parent f4af54e commit 954d7a7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

frontend/angular-best-practices.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ The following table summarizes the module types.
2727

2828
The "Rare" means that noramlly you should not provider definitions in that type.
2929

30+
To prevent a service module provided by a lazy loading moudle, use the following code in a service module:
31+
32+
```ts
33+
constructor (@Optional() @SkipSelf() parentModule: CoreModule) {
34+
if (parentModule) {
35+
throw new Error(
36+
'CoreModule is already loaded. Import it in the AppModule only');
37+
}
38+
}
39+
```
40+
41+
A module whose class defined with the above constructor will throw an exception when it is provided more than once.
42+
43+
It is not recommended that a module provides services and declares declarables. If that happens, such as the `RouterModule`, use `forRoot()` to provide and config services for the root module and `forChild()` for other modules.
44+
3045
## Architecture: Smart Components and Presentational Components
3146

3247
The overall Angular application could be organized into two types of components: [smart components and presentational components](https://blog.angular-university.io/angular-2-smart-components-vs-presentation-components-whats-the-difference-when-to-use-each-and-why/).

0 commit comments

Comments
 (0)