6

I am developing a large application using Angular 2 and ASP.net MVC. I have approximately 120 components in my application which are all declared in @ngModule declarations block:

@NgModule({
  imports: [CommonModule],
  declarations: [Component1, Component2, Component3, ..., Component120]
})

Is there a mechanism where I can shorten this?

1
  • Breakdown your application to smaller smaller module/submodule, think in such a way that each module should not have more than 10-15 components, the you can create NgModule for each submodule & load them lazily via router feature(this can also add boost in application performance). Commented Nov 22, 2016 at 10:28

2 Answers 2

11

You can build a shared module and add the components to this module, then you only have to add the shared module to imports: [MySharedModule] and all components and directives exported by MySharedModule become available to the importing module.

@NgModule({
  imports: [ CommonModule ],
  declarations: [ Component1,   Component2,   Component3....      Component120 
],
  exports: [ Component1,   Component2,   Component3....      Component120 ],
})

export class MySharedModule {}

@NgModule({
  declarations: [AppComponent],
  bootstrap: [AppComponent],
  imports: [BrowserModule, MySharedModule]
})
class AppModule {}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the suggestion, will implement in this way.
You have imports: [ CommonModule ], twice.
-2

I'm afraid there is not a shorter way to achieve what you are trying to do.

But if it is possible, you can divide your application in multiple parts. This means you will have more than one app.module.ts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.