被依赖的共享? [英] Are dependencies shared?

查看:121
本文介绍了被依赖的共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有3个模块:

Let's say I have 3 modules:

angular.module('A', [])

angular.module('B', ['A'])

angular.module('C', ['B', 'A'])

在依赖注入方面,将 B C 共享模块 A ,或将不同的实例被注入到每个人?

In terms of dependency injection, will B and C share the same instance of module A, or will separate instances be injected into each of them?

推荐答案

要总结你的问题,答案应该是只有一个实例。

To summarize your question, answer would be only one instance.

在事实上,它是这个样子。角应用通过喷油器解决依赖。只有一个喷油器每个应用程序创建的。从技术上讲,你只能有一个 NG-应用,但您可以通过使用手动引导有多个应用程序,在这种情况下,将有每个应用程序创建的注射器以及2应用不会分享任何相关性。

In fact it is this way. Angular app resolves dependencies via injector. Only one injector is created per app. Technically you can have only one ng-app, but you can have multiple apps by using manual bootstrapping, in that case there will be an injector created for each app and those 2 apps will not share any dependencies.

在那里只有rootElement的自举的作为角度应用一般情况。它解析模块开始已自举的模块(从依赖关系链的底部开始)。任何服务/控制器/过滤器等该模块或依赖关系链下的任何依赖模块下注册将在喷油器缓存(注射时懒洋洋地实例化),只有一次一起捆绑销售。例如说,你有为myService 服务模块下注册 A 。不管你多少地方列出 A 作为依赖它的构造,最终只有一个实例将提供和服务作为一个单身每个人都得到相同的单一实例。

In general case where there is only rootElement bootstrapped as an angular app. It resolves the modules starting the module that has been bootstrapped (starting from the bottom of the dependency chain). Any services/controllers/filters etc registered under that module or any dependent modules under that dependency chain will be bundled together in the injector cache(instantiated lazily when injected) just once. For example say you have a service myService registered under module A. No matter how many places you list A as dependency ultimately only one instance of its constructor will be available, and service being a singleton everyone gets the same singleton instance.

官方文件

模块可以列出其他模块,它们的依赖。根据一个模块上意味着所需模块需要被装载到要求模块之前被加载。换句话说所需模块的配置块需要模块的配置块之前执行。这同样适用于在运行块真。每个模块只能加载一次,即使多个其他模块需要它。

Modules can list other modules as their dependencies. Depending on a module implies that the required module needs to be loaded before the requiring module is loaded. In other words the configuration blocks of the required modules execute before the configuration blocks of the requiring module. The same is true for the run blocks. Each module can only be loaded once, even if multiple other modules require it.

这篇关于被依赖的共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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