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

天大地大妈咪最大 提交于 2019-12-11 01:34:02

问题


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)

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/41563723/what-is-the-benefit-of-using-multiple-modules-in-angular2-apart-from-simplicity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!