Angularjs – 如何从不依赖的另一个模块更正注入服务?

前端之家收集整理的这篇文章主要介绍了Angularjs – 如何从不依赖的另一个模块更正注入服务?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@ 我不明白如何工作模块化依赖.

我有4个模块,它们依赖于彼此,如图所示.

“App”模块包括“module1”和“module2”.
“module2”包含“核心”模块. There are source on plunker.

如果从模块核心到模块“module1”的注入服务工作正常.但“核心”模块不在模块“module1”中.为什么会发生

由于您的App模块依赖于Core模块(间接通过模块2),Core模块中的服务可以在App模块(包括模块1)的任何位置使用.

这是因为Angular将首先加载所有模块,然后开始实例化其组件并解决注入的依赖关系.

但是,如果您确实需要模块1中的Core服务,那么您应该使其依赖于Core模块.这样,您的应用程序不会破坏,如果模块2在以后修改(或完全删除),您的模块1将更加自包含和可重用(例如,您可以使用不同的应用程序不依赖于核心模块).

一般来说,你不应该依靠“间接”依赖.每个模块都应该显式地声明它的依赖关系.
角度足够聪明,只有加载模块,如果它还没有加载,所以没有开销.

引用开发者指南section on modules

Modules can list other modules as their dependencies. Depending on a module implies that 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.

(强调我的)

原文链接:https://www.f2er.com/angularjs/140502.html

猜你在找的Angularjs相关文章