简体   繁体   English

Angular 2和ngmodule注射器

[英]Angular 2 and ngmodule injector

NGmodule gives a way to organise our code in modules. NGmodule提供了一种在模块中组织代码的方法。

It also proposes to provides some services as a property of the module and I dont understand the way it works. 它还建议提供一些服务作为模块的属性,我不理解其工作方式。

First does it mean that if I add a service in the provides property of a NGmodule, it will expose the service (meaning that I have to call the module inside of another one to use its services ) ? 首先它的意思是,如果我添加的一个服务提供的NGmodule的属性,它会暴露的服务(这意味着我必须调用模块另外一个内部使用的服务)?

And so, is there a NGmodule injector level ? 因此,是否存在NGmodule喷射器液位?

How Can I use a service outside the module box in another module ? 如何在另一个模块的模块盒外使用服务?

Providing a service in a modules means that the service will be instantiated and made available to all components, directives, and pipes that are part of the module. 在模块中提供服务意味着该服务将被实例化并提供给该模块中的所有组件,指令和管道。 The word 'instantiation' is key here - since services are singletons, a module must keep track of services for every components, directive, or pipe that uses them. 此处的“实例化”是关键-由于服务是单例,因此模块必须跟踪使用它们的每个组件,指令或管道的服务。 You could also provide the service in individual components, but that would instantiate the service each in each component, effectively negating the reason why we would use a singleton service in the first place. 您也可以在单个组件中提供服务,但是这将实例化每个组件中的每个服务,从而有效地消除了我们首先使用单例服务的原因。 Providing it at the module-level solves this problem for us. 在模块级别提供它可以为我们解决这个问题。

If you want to make your service available outside of your module, then don't have to do anything. 如果您想使服务在模块之外可用,则无需执行任何操作。 Providing the service within a module that is imported in your project means that it's already available anywhere in your project. 在项目中导入的模块中提供服务意味着该服务已在项目中的任何位置可用。

Related: Why is a service provided in a feature module available everywhere? 相关: 为什么功能模块中提供的服务随处可见?

If you wanted to make components, directives, or pipes available outside of your module, then you have to export them (and import them in the module where you want to use them). 如果要使组件,指令或管道在模块外部可用,则必须导出它们(并将它们导入要使用它们的模块中)。 You can do that by using the export keyword in your module. 您可以通过在模块中使用export关键字来实现。

Related: What classes should I export? 相关: 我应该导出哪些类?

For example, you can use the NgIf directive because it's exported from the CommonModule ( docs ), which we then import in our own modules: 例如,您可以使用NgIf指令,因为它是从CommonModule( docs )导出的,然后我们将其导入我们自己的模块中:

@NgModule({
  declarations: [COMMON_DIRECTIVES, COMMON_PIPES],
  exports: [COMMON_DIRECTIVES, COMMON_PIPES],
  providers: [
    {provide: NgLocalization, useClass: NgLocaleLocalization},
  ],
})

Fun fact: if you only have one module (the root module that's bootstrapped), then you would actually only use the BrowserModule instead of the CommonModule. 有趣的事实:如果只有一个模块(引导的根模块),则实际上只使用BrowserModule而不是CommonModule。 The reason all of the functionality of CommonModule is in BrowserModule is because BrowserModule just imports and the re-exports the entire CommonModule. CommonModule的所有功能都在BrowserModule中的原因是因为BrowserModule只是导入并重新导出整个CommonModule。

There's a great in-depth guide to the module system on angular2's website if you want more info. 如果您需要更多信息,可以在angular2的网站上找到有关模块系统的深入指南。 The FAQ page that I linked before is also super useful. 我之前链接的FAQ页面也很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM