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

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

问题描述

是否有助于减少加载时间?(比如只加载当前模块到客户端的设备?)

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

我的理解是...

  • 它会增加服务器和客户端之间的请求数量吗?
  • 它会增加你的代码的复杂性.(因为你需要采取照顾
  • 导入和导出模块并特别注意路由)

推荐答案

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

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',
},

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

更新

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

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天全站免登陆