Skip to content

Commit b795375

Browse files
committed
add resolve the conflict of module
1 parent b8720dc commit b795375

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

frontend/angular-best-practices.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ A module whose class defined with the above constructor will throw an exception
4242

4343
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.
4444

45+
46+
### 解决 Module 冲突
47+
48+
出现场景:
49+
50+
- 在订单处理模块中有一个国内机票模块,我们命名为:
51+
- 文件相对路径以及文件名:`./flight/flight.module.ts`
52+
- `Module` 名: `FlightModule`
53+
- 在基础数据模块中同样有一个国内机票模块,命名也和订单处理一致。
54+
- 在订单处理模块和基础数据模块的 `RoutingModule` 中使用懒加载:
55+
56+
```ts
57+
{
58+
path: 'flight',
59+
loadChildren: './flight/flight.module#FlightModule',
60+
},
61+
```
62+
63+
那么就回会出现下面的错误提示:
64+
65+
```bash
66+
ERROR in Duplicated path in loadChildren detected: "./domestic-flight/domestic-flight.module#DomesticFlightModule" is used in2 loadChildren, but they point to different modules "(/Users/vm/Workspace/Webs/Angular/tehang-system/src/app/routes/order/domestic-flight/domestic-flight.module.ts and "/Users/vm/Workspace/Webs/Angular/tehang-system/src/app/routes/basic-resource/domestic-flight/domestic-flight.module.ts"). Webpack cannot distinguish on context and would fail to load the proper one.
67+
```
68+
69+
通过错误提示,我们可以得知问题的原因是由于 `Webpack` 无法正确的区分它们。
70+
那么解决办法有两个:
71+
72+
- 在 `RoutingModule` 中的 `loadChildren` 添加一个父级路径,例如:
73+
74+
```ts
75+
// 基础数据模块的 `RoutingModule`
76+
{
77+
path: 'flight',
78+
loadChildren: '../basic-resource/flight/flight.module#FlightModule',
79+
},
80+
```
81+
82+
- 把其中一个国内机票模块改个名字(不仅仅是改 `FlightModule` 这个名字, 还要改文件名)
83+
84+
我们建议使用第一种解决方式,理由是:① 重新命名需要修改的地方较多。 ② 命名是编程两大难题之一。
85+
4586
## Architecture: Smart Components and Presentational Components
4687
4788
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)