what is the benefit of using multiple modules in Angular2 apart from simplicity in designing?

2.3k views Asked by At

Will it help to reduce the loading time?(like by loading only current module to client's device? )

what I understand is that...

  • It will increase the number of request between server and client?
  • It will increase the complexity of your code.(Since you need to take care of
  • importing and exporting modules and take special care in routing too)
2

There are 2 answers

14
Günter Zöchbauer On BEST ANSWER

Angular supports lazy loading of modules. This way you can split up your application into parts that are loaded only on demand.

{
  path: 'admin',
  loadChildren: 'app/admin/admin.module#AdminModule',
},

See also https://angular.io/docs/ts/latest/guide/router.html#!#asynchronous-routing

update

While lazy loading doesn't reduce the load time when everything is required at the start page which is IMHO not the case in almost 100% of Angular2 applications, it reduces the time for the initial load and delays the load time to when a module is actually required. Angular also supports preload feature to load lazy loaded modules before they are required, but still doesn't load them with the initial load.

  • It will increase the number of request between server and client?

The number of requests will be higher, but the amount of data loaded with the first request will be less. The main idea is to reduce the time until the user gets the first screen rendered. If an applications consists of parts where some are heavily used and other rarely, then it's usually a good idea to not load the rarely used parts until they are actually used - which means "lazy" or "on demand" - only when actually required.

  • It will increase the complexity of your code.(Since you need to take care of

If you want to gain full benefit of lazy loading, you will need to think about your architecture. That doesn't mean the code needs to become more complex. You just need to make decisions which module you put your components, directives, services into.

  • importing and exporting modules and take special care in routing too)

I don't see much difference here, because you should split your application in different modules (for example per feature) anyway, even if they are not lazy loaded.

10
Amid On

I do not think its angular specific question. There are tons of answers why you should use modules if you google for them, for example have a look here: modules

Now short answers to your question:

  • it will not help to reduce the loading time
  • it will increase the number of requests
  • it will actually help to decrease the complexity of the code by decoupling non-related functionality and not having to pollute global namespaces