我应该使用角度模块吗?

时间:2017-04-18 09:42:28

标签: javascript angular module

我正在学习 angular4 。我在某处读到某些版本的angular2使用@NgModule引入了模块。

假设我正在创建一个会计应用程序。所以,会有不同的功能,如组,分类账等。所以,基于功能我可以将它分成GroupModule,LedgersModule等模块。所以,假设我首先创建我的GroupsModule,它看起来像:

GroupsModule
  |--Classes
       |--Group.ts
  |--Components
       |--Group-list
            |--Group-list.component.ts
            |--Group-list.component.html
            |--Group-list.component.css
       |--Group-create
            |--Group-create.component.ts
            |--Group-create.component.html
            |--Group-create.component.css
  |--Services
       |--Group.service.ts
  |--Group.Module.ts

现在如果我创建Ledger模块,那么我将不得不重构GroupsModule并将其部分文件移动到SharedModule,因为我的LedgersModule需要访问GroupsModule的某些部分,所以现在我的GroupsModule将如下所示:

GroupsModule
  |--Components
       |--Group-list
            |--Group-list.component.ts
            |--Group-list.component.html
            |--Group-list.component.css
       |--Group-create
            |--Group-create.component.ts
            |--Group-create.component.html
            |--Group-create.component.css
  |--Group.Module.ts

LedgersModule看起来像:

LedgersModule
  |--Classes
       |--Ledger.ts
  |--Components
       |--Ledger-list
            |--Ledger-list.component.ts
            |--Ledger-list.component.html
            |--Ledger-list.component.css
       |--Group-create
            |--Ledger-create.component.ts
            |--Ledger-create.component.html
            |--Ledger-create.component.css
  |--Services
       |--Ledger.service.ts
  |--Ledger.Module.ts

SharedModule将如下所示:

SharedModule
  |--Classes
       |--Group.ts
  |--Services
       |--Group.service.ts
  |--Shared.Module.ts

在这种方法中,我有两个问题:

问题1:

我必须继续重构我已完成的模块,以便其他模块可以使用它们。

问题:2

我已将模块中的代码分开,以便我可以将代码保密。

例如,如果我创建另一个名为SomeOtherModule的模块,需要使用LedgersModule的某些部分。所以,现在我必须重构LedgersModule并在SharedModule中移动一些文件。现在SomeOtherModule将引用SharedModule,它将可以访问所需的Ledger文件。但现在SomeOtherModule也可以使用 Group.service.ts ,因为它引用了SharedModule。所以,我的代码现在不是私密的。我不希望SomeOtherModule能够访问GroupModule中的{原始} SharedModule文件。

在我的应用程序中,所有或大多数模块都依赖于其他模块。但是这些模块应该只允许访问他们需要的设施,而不仅仅是那些。

那我为什么要使用模块呢?没有模块,我可以做同样的事情。

你能建议我在这种情况下使用模块吗?或者只使用AppModule并在那里做所有事情会更好吗?

0 个答案:

没有答案
相关问题