在角度中使用多个模块有什么好处? [英] What is the benefit of using multiple modules in angular?

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

问题描述

我是Angular Js熟悉的人,最近我看到在一些项目中,在主模块中创建并组装了多个角度模块。

I'm a familiar person with Angular Js, recently I've seen that in some projects multiple angular modules are created and assembled in main module.

代码看起来像。

angular.module("main",['main.sub1','main.sub2','main.sub2'])

angular.module("main.sub1",[])

angular.module("main.sub2",[])

angular.module("main.sub3",[])

我的问题是


  1. 什么时候接近这种分裂模块的方式?

  2. 它是如何有用的?

  3. 这是否会影响路由[routeProvider / stateProvider](因为模块的定义不同,我可以单独为每个模块提供路由提供者或状态提供者)

  4. 如果我注入了默认情况下,子模块中的依赖项是否在主模块中注入?

  1. When to approach such fashion of splitting modules ?
  2. How it is useful ?
  3. Does this affect routing[routeProvider/stateProvider] (since modules are defined differently can i place route provider or state provider for each separately)
  4. If i inject a dependency in sub modules are they by default injected in main modules ?

我读了几篇博客,但我无法弄清楚感知。

I read few blogs but I'm not able to get a clear perception.

请你指导我gh。

推荐答案


1.何时接近分裂模块的方式?

1.When to approach such fashion of splitting modules ?

最好是始终。但是你真的不需要这样做,除非你有多个使用完全相同的代码的应用程序,也许你在几个应用程序中处理身份验证的方式是相同的,将公共代码放入服务并拥有它是有意义的该服务作为自己的模块。这提供了代码重用,我们都知道代码重用是好的。

Preferably, Always. But you don't really need to do it unless you have multiple apps that are using the exact same code, maybe the way you handle authentication in a few apps is identical, it would make sense to put the common code into a service and have that service as its own module. This offers code reuse, and we all know that code reuse is good.


2.它如何有用?

2.How it is useful ?

同样,可重用性是关键词。你不想四处寻找重复的代码。代码重复是完全错误的,导致更昂贵的维护和更容易出错的代码。
另一个优点是模块化应用程序显式概述了应用程序的依赖关系,并分离了应用程序不同部分的职责。代码重用和关注点分离是编写可维护代码的基本实践。

Again, reusability is the key word here. You don't want to go around writing duplicate code. Code duplication is just plain wrong, and results in more expensive maintenance and more error prone code. Another advantage is that modularising your applications explicitly outlines the dependencies of your application and separates the responsibilities of different parts of your app. Code reuse and separation of concerns are fundamental practices in writing maintainable code.

将应用程序分成模块的另一个原因是性能。现在我正在开发一个应用程序(网上商店),它由2个主要部分组成:一个部分(app)适用于普通用户/买家/卖家,另一个应用程序适用于管理员。现在,用户应用程序需要管理员应用程序不需要的一些脚本/库,反之亦然。例如,管理员应用程序使用一个kendo网格,需要一个 kendo.all.min.js 脚本,当缩小时为1.7MB!现在强制所有访问者访问网站下载重1.7 MB的脚本是否合理?

Another reason to separate your app into modules is performance. Right now I'm working on an app (web shop) which consists of 2 main sections: one section(app) for normal users/buyers/sellers, and another app for the admin. Now the users app needs some scripts/libraries the admin app doesn't, and vice versa. For example the admin app uses a kendo grid which requires a kendo.all.min.js script which when minified is 1.7MB! Now would it make sense to force all visitors to the site to download the heavy 1.7 MB script?


3.这是否会影响路由[routeProvider] / stateProvider](由于模块的定义不同,我可以单独为路由提供者或州提供者提供

3.Does this affect routing[routeProvider/stateProvider] (since modules are defined differently can i place route provider or state provider for each separately)

是。理想情况下,您的应用程序将具有不同的路由/状态配置,因此每个应用程序都有自己的 app.config(),这意味着您可以使用共享为每个应用程序定义单独的路由模块。根据我的经验,你想要进入他们自己的模块的主要内容是服务和指令,而不是应用程序路由。

Yes. Ideally your applications will have different route/state configurations, so each app would have their own app.config(), this means you could define separate routes for each app using the shared modules. To my experience the main things you want to move into their own modules are services and directives, not applications routings.


4.如果我注入默认情况下,子模块中的依赖项是否在主模块中注入?

4.If i inject a dependency in sub modules are they by default injected in main modules ?

是。如果将依赖项 X 注入模块 A ,并且模块 A 将由另一个模块 B 使用,然后 B 也将继承依赖 X

Yes. If you inject a dependency X into a module A, and module A will be used by another module B then B will also inherit the dependency X.

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

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