加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:42:17 所属栏目:安全 来源:网络整理
导读:我不明白如何工作模块化依赖. 我有4个模块,它们依赖于彼此,如图所示. “App”模块包括“module1”和“module2”. “module2”包含“核心”模块. There are source on plunker. 如果从模块核心到模块“module1”的注入服务工作正常.但“核心”模块不在模块“m
我不明白如何工作模块化依赖.

我有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.

(强调我的)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读