简体   繁体   English

Angular 2进样器层次结构和NgModule

[英]Angular 2 injector hierarchy and NgModule

I wonder how NgModule actually affects Angular 2 injector hierarchy. 我不知道NgModule实际上如何影响Angular 2注入器层次结构。

What does the hierarchy look like in an app with nested modules? 在具有嵌套模块的应用程序中,层次结构是什么样的? Does it create a new injector instance per each module or gets access to top-level injector (similarly to Angular 1.x modules)? 它是否为每个模块创建一个新的注射器实例,还是可以访问顶级注射器(类似于Angular 1.x模块)?

It can be somewhat confusing to figure out the tree in a big app. 在大型应用程序中弄清楚这棵树可能有些令人困惑。

Is there a way to print out, inspect or explore visually the hierarchy of injectors (like it could be done for scope hierarchy in Angular 1.x)? 有没有一种方法可以打印出,检查或直观地查看注射器的层次结构(就像可以在Angular 1.x中对作用域层次结构进行操作一样)?

According to the Modules documentation: https://angular.io/docs/ts/latest/guide/ngmodule.html 根据模块文档: https : //angular.io/docs/ts/latest/guide/ngmodule.html

Angular registers these providers with the root injector of the module's execution context. Angular将这些提供程序注册到模块执行上下文的根注入器中。 That's the application's root injector for all modules loaded when the application starts. 这是应用程序启动时加载的所有模块的应用程序的根注入器。

Angular can inject one of these provider services into any component in the application. Angular可以将这些提供程序服务之一注入到应用程序的任何组件中。 If this module provides the HeroService, or any module loaded at launch provides the HeroService, Angular can inject the same HeroService intance into any app component. 如果此模块提供了HeroService,或者启动时加载的任何模块都提供了HeroService,则Angular可以将相同的HeroService实例注入任何应用程序组件中。

A lazy loaded module has its own sub-root injector which typically is a direct child of the application root injector. 延迟加载的模块具有自己的子根注入器,它通常是应用程序根注入器的直接子代。

Lazy loaded services are scoped to the lazy module's injector. 延迟加载的服务仅限于延迟模块的注入器。 If a lazy loaded module also provides the HeroService, any component created within that module's context (eg, by router navigation) gets the local instance of the service, not the instance in the root application injector. 如果延迟加载的模块还提供HeroService,则在该模块的上下文中创建的任何组件(例如,通过路由器导航)都将获取服务的本地实例,而不是根应用程序注入器中的实例。

Components in external modules continue to receive the instance created for the application root. 外部模块中的组件继续接收为应用程序根目录创建的实例。

So, you have one injector that is shared between all the modules. 因此,您有一个在所有模块之间共享的注射器。 However, lazy-loaded components will have a child injector 但是,延迟加载的组件将具有子注入器

You should be able to investigate providers using ng.probe($0) in the browser console. 您应该能够在浏览器控制台中使用ng.probe($0)调查提供程序。 See also https://stackoverflow.com/a/35233711/217408 . 另请参阅https://stackoverflow.com/a/35233711/217408

You can load modules with MyModule.forRoot() (see also https://angular.io/docs/ts/latest/guide/ngmodule.html#!#providers ) to get providers added at the root level. 您可以使用MyModule.forRoot()加载模块(另请参见https://angular.io/docs/ts/latest/guide/ngmodule.html#!#providers ),以在根级别添加提供程序。

If you provide it in a module, then it's available to the components in this module. 如果您在模块中提供它,那么该模块中的组件可以使用它。 I haven't seen it mentioned that a module introduces a child injector explicitly but to me this looks like it has to be the case. 我还没有看到它提到模块明确地引入了子注入器,但是在我看来这确实是必须的。

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

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