除了设计简单之外,在Angular2中使用多个模块还有什么好处? [英] what is the benefit of using multiple modules in Angular2 apart from simplicity in designing?

查看:135
本文介绍了除了设计简单之外,在Angular2中使用多个模块还有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

减少加载时间是否有帮助?(例如,仅将当前模块加载到客户端设备上?)

我了解的是...

  • 这会增加服务器和客户端之间的请求数量吗?
  • 这将增加代码的复杂性.(因为您需要 照顾
  • 导入和导出模块,并且在路由方面也要格外小心

解决方案

Angular支持模块的延迟加载.这样,您可以将应用程序拆分为仅按需加载的部分.

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

另请参见 https://angular.io/docs/ts/latest/guide/router.html#!#asynchronous-routing

更新

虽然在开始页面上需要所有内容时,延迟加载不会减少加载时间(恕我直言,在几乎100%的Angular2应用程序中不是这种情况),但它减少了初始加载的时间并将延迟的加载时间延迟到实际上需要一个模块. Angular还支持预加载功能,可以在需要之前加载延迟加载的模块,但仍不会在初始加载时加载它们.

  • 这会增加服务器和客户端之间的请求数量吗?

请求数量将增加,但第一个请求加载的数据量将减少.主要思想是减少用户获得第一个屏幕渲染之前的时间.如果一个应用程序包含一些经常使用而很少使用的部件,那么通常最好不要在真正使用它们之前加载很少使用的部件-这意味着懒惰"或按需"-仅在实际需要时才加载它们.

  • 这会增加代码的复杂性.(因为您需要注意

如果您想充分利用延迟加载的优势,则需要考虑一下您的体系结构.这并不意味着代码需要变得更加复杂.您只需要确定将组件,指令,服务放入哪个模块中即可.

  • 导入和导出模块,并且在路由时也要格外小心

我在这里看不出太大的区别,因为即使您的应用程序不是延迟加载的,您也应该将其拆分为不同的模块(例如,每个功能).

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)

解决方案

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.

这篇关于除了设计简单之外,在Angular2中使用多个模块还有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆