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

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

问题描述

我对 Angular Js 很熟悉,最近我看到在一些项目中,在主模块中创建并组装了多个 Angular 模块.

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. 如果我在子模块中注入依赖项,它们是否默认注入到主模块中?

我读了一些博客,但我无法清楚地了解.

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

你能指导我完成吗.

推荐答案

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.它有什么用?

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

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 个主要部分组成:一个部分(应用程序)供普通用户/买家/卖家使用,另一个应用程序供管理员使用.现在用户应用程序需要一些管理应用程序不需要的脚本/库,反之亦然.例如,管理应用程序使用剑道网格,它需要一个 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.

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

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