|
2 | 2 |
|
3 | 3 | Here is a set of best practices using Angular.
|
4 | 4 |
|
| 5 | +## Types of Angular Modules |
| 6 | + |
| 7 | +An Angular module, called NgModule or simply module, is a class marked by the `@NgModule` decorator. The modules can be classified into the following types. |
| 8 | + |
| 9 | +- The Root module: it is the root `AppModule` that lauches the app. This module be as simple as possible. For exaple, it should only use one, not many, top-level feature module to organize the UI. |
| 10 | +- Feature module: feature modules handles UI features. It has two sub types. |
| 11 | + - eager-loading feature modules: these modules provide the UI features required in application start. Usually a feature module has a top component and many supporting sub-components. It declares and exports the top componnent but hides the sub-components. It is usually imported by the root module and other eager-loading modules. For simplicity, it is called eager-loading module or eager module. |
| 12 | + - routed feature module: these modules proide the UI features that are targets of navigation routes. They can be preloaded or lazy loaded. They don't declare and export components because they are loaded by the router and are usd independently. They are also called as routed modules. |
| 13 | +- Routing module: a routing module provides routing configuration for a routed module and should have a matching name. For example, the root module `AppModule` has a routing module `AppRoutingModule`. A routed moudle `FooModule` should have a routing module `FooRoutingModule`. It defines routes, guards and resolver servcie providers. It should re-exports the `RouterModule` thus its corresponding routed mdoule can use route services. To configure routes, the `AppModule` calls `RouterModule.forRoot(routes)` and a feature module calls `RouterModule.forChid(routes)`. A routing module should not have declarations. |
| 14 | +- Service module: a service module provides utility services such as data access. It should only has providers and have no declarations. The root `AppModule` is the only module that should import service modules. If a service is only used locally in a feature module, then it should be defined as a service class, not in a module, inside the feature module folder. |
| 15 | +- Widget module: a widget module defines shareable components, directives, and pipes for other feature modules. It should only have exported declarables and should not have providers. |
| 16 | + |
| 17 | +The following table summarizes the module types. |
| 18 | + |
| 19 | +| Type | Declarations | Providers | Exports | Imported by | |
| 20 | +| ------- | ------------ | ------------ | -------------- | ------------- | |
| 21 | +| Root | No | No | No | None | |
| 22 | +| Eager | Yes | Rare | Top components | Root, Feature | |
| 23 | +| Routed | Yes | Rare | No | None | |
| 24 | +| Routing | No | Yes (Guards) | RouterModule | Root, Feature | |
| 25 | +| Service | No | Yes | No | Root | |
| 26 | +| Widget | Yes | Rare | Yes | Feature | |
| 27 | + |
| 28 | +The "Rare" means that noramlly you should not provider definitions in that type. |
| 29 | + |
5 | 30 | ## Architecture: Smart Components and Presentational Components
|
6 | 31 |
|
7 | 32 | 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/).
|
@@ -89,7 +114,7 @@ this.service
|
89 | 114 |
|
90 | 115 | 建议采用方式一的原因在最后一步分别处理正常和错误数据而不用顾虑处理结果, 避免了方式二为了保证统一输出所需要的`of()`转换。
|
91 | 116 |
|
92 |
| -建议`pipe()`只用于调试,log 或需要数据转换但不能用`subscribe()`的场合。 |
| 117 | +建议`pipe()`只用于调试,log,延时,错误重试或需要数据转换但不能用`subscribe()`的场合。 |
93 | 118 |
|
94 | 119 | 当运行后台任务时需要显示 Spinner 或 loading 信息。建议采用如下模式:
|
95 | 120 |
|
|
0 commit comments