核心模块的指令混淆服务使用

时间:2017-02-13 09:00:14

标签: angular angular-directive angular-services ng-modules

我有一个名为CoreModule的核心模块,它包含一个名为MapManagerService的服务,名为FlyToDirective的指令使用MapManagerService,以及一个名为ElementComponent的组件flyTo使用AppModule

我的CoreModule导入MapManagerService并使用其'组件,加上它的'拥有FlyToDirective

阅读Angular的documentation核心模块,您会注意到:

  

更确切地说,Angular会在追加@ NgModule.providers中列出的项目之前累积所有导入的提供程序。此序列确保我们向AppModule提供程序显式添加的内容优先于导入模块的提供程序。

但是,CoreModule仍然使用导入的MapManagerService' AppModule,我的文档错误了吗?

我认为应该使用的服务是MapManagerService s CoreModule而不是<header id="jHeader" class="invert"> <!-- NavBar --> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="index.html"> <img src="images/IE_logo.png" alt="logo"> </a> </div> <!-- Links --> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav navbar-right invert"> <li class="active"><a href="index.html">home</a></li> <li><a href="about-minimal.html">About Us</a></li> <!--<li><a href="The Intrepid Earth Process.html">Portfolio</a></li>--> <li><a href="The Intrepid Earth Process.html">The Intrepid Earth Process</a></li> <li><a href="contact.html">contact</a></li> </ul> </div> <!-- Links --> </nav> <!-- NavBar --> </header> s

任何帮助都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

添加到非延迟加载模块的providers: [...]的所有提供程序都会被提升到应用程序根范围。在多个这样的模块中注册了相同的提供程序,只有最后导入的模块生效。

添加到AppModule的提供程序直接优先于导入模块的提供程序。

您可以向@Component({providers: [...]})添加提供程序,然后组件及其后代从此提供程序获取实例,而不是应用程序根范围的提供程序。

或者,您可以使模块延迟加载。延迟加载的模块在此模块中获取自己的根范围,组件和服务,而不是来自此提供程序的实例。