# NgModule API #### Prerequisites #### 前提条件 A basic understanding of the following concepts: 对下列概念有基本的理解: * [Bootstrapping](guide/bootstrapping). [引导启动](guide/bootstrapping)。 * [JavaScript Modules vs. NgModules](guide/ngmodule-vs-jsmodule). [JavaScript 模块与 NgModules](guide/ngmodule-vs-jsmodule)。
Property 属性 | Description 说明 |
---|---|
declarations
|
A list of [declarable](guide/ngmodule-faq#q-declarable) classes,
(*components*, *directives*, and *pipes*) that _belong to this module_.
*属于该模块*的[可声明对象](guide/ngmodule-faq#q-declarable)(*组件*、*指令*和*管道*)的列表。
|
providers
|
A list of dependency-injection providers. 依赖注入提供商的列表。 Angular registers these providers with the NgModule's injector. If it is the NgModule used for bootstrapping then it is the root injector. Angular 会使用该模块的注入器注册这些提供商。 如果该模块是启动模块,那就会使用根注入器。 These services become available for injection into any component, directive, pipe or service which is a child of this injector. 当需要注入到任何组件、指令、管道或服务时,这些服务对于本注入器的子注入器都是可用的。 A lazy-loaded module has its own injector which is typically a 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 `UserService`, any component created within that module's context (such as by router navigation) gets the local instance of the service, not the instance in the root application injector. 惰性加载的服务是局限于这个惰性加载模块的注入器中的。 如果惰性加载模块也提供了 `UserService`,那么在这个模块的上下文中创建的任何组件(比如在路由器导航时),都会获得这个服务的本模块内实例,而不是来自应用的根注入器的实例。 Components in external modules continue to receive the instance provided by their injectors. 其它外部模块中的组件也会使用它们自己的注入器提供的服务实例。 For more information on injector hierarchy and scoping, see [Providers](guide/providers) and the [DI Guide](guide/dependency-injection). 要深入了解关于多级注入器及其作用域,参见[服务提供商](guide/providers)。 |
imports
|
A list of modules which should be folded into this module. Folded means it is as if all the imported NgModule's exported properties were declared here. 要折叠(Folded)进本模块中的其它模块。折叠的意思是从被导入的模块中导出的那些软件资产同样会被声明在这里。 Specifically, it is as if the list of modules whose exported components, directives, or pipes are referenced by the component templates were declared in this module. 特别是,这里列出的模块,其导出的组件、指令或管道,当在组件模板中被引用时,和本模块自己声明的那些是等价的。 A component template can [reference](guide/ngmodule-faq#q-template-reference) another component, directive, or pipe when the reference is declared in this module or if the imported module has exported it. For example, a component can use the `NgIf` and `NgFor` directives only if the module has imported the Angular `CommonModule` (perhaps indirectly by importing `BrowserModule`). 组件模板可以[引用](guide/ngmodule-faq#q-template-reference)其它组件、指令或管道,不管它们是在本模块中声明的,还是从导入的模块中导出的。 比如,只有当该模块导入了 Angular 的 `CommonModule(也可能从 `BrowserModule` 中间接导入)时,组件才能使用 `NgIf` 和 `NgFor` 指令。 You can import many standard directives from the `CommonModule` but some familiar directives belong to other modules. For example, you can use `[(ngModel)]` only after importing the Angular `FormsModule`. 你可以从 `CommonModule` 中导入很多标准指令,不过也有些常用的指令属于其它模块。 比如,你只有导入了 Angular 的 `FormsModule` 时才能使用 `[(ngModel)]`。 |
exports
|
A list of declarations—*component*, *directive*, and *pipe* classes—that an importing module can use. 可供导入了自己的模块使用的可声明对象(**组件**、**指令**、**管道类**)的列表。 Exported declarations are the module's _public API_. A component in another module can [use](guide/ngmodule-faq#q-template-reference) _this_ module's `UserComponent` if it imports this module and this module exports `UserComponent`. 导出的可声明对象就是本模块的*公共 API*。 只有当其它模块导入了本模块,并且本模块导出了 `UserComponent` 时,其它模块中的组件才能[使用](guide/ngmodule-faq#q-template-reference)*本*模块中的 `UserComponent`。 Declarations are private by default. If this module does _not_ export `UserComponent`, then only the components within _this_ module can use `UserComponent`. 默认情况下这些可声明对象都是私有的。 如果本模块*没有*导出 `UserComponent`,那么就只有*本*模块中的组件才能使用 `UserComponent`。 Importing a module does _not_ automatically re-export the imported module's imports. Module 'B' can't use `ngIf` just because it imported module 'A' which imported `CommonModule`. Module 'B' must import `CommonModule` itself. 导入某个模块*并不会*自动重新导出被导入模块的那些导入。 模块 B 不会因为它导入了模块 A,而模块 A 导入了 `CommonModule` 而能够使用 `ngIf`。 模块 B 必须自己导入 `CommonModule`。 A module can list another module among its `exports`, in which case all of that module's public components, directives, and pipes are exported. 一个模块可以把另一个模块加入自己的 `exports` 列表中,这时,另一个模块的所有公共组件、指令和管道都会被导出。 [Re-export](guide/ngmodule-faq#q-reexport) makes module transitivity explicit. If Module 'A' re-exports `CommonModule` and Module 'B' imports Module 'A', Module 'B' components can use `ngIf` even though 'B' itself didn't import `CommonModule`. [重新导出](guide/ngmodule-faq#q-reexport)可以让模块被显式传递。 如果模块 A 重新导出了 `CommonModule`,而模块 B 导入了模块 A,那么模块 B 就可以使用 `ngIf` 了 —— 即使它自己没有导入 `CommonModule`。 |
bootstrap
|
A list of components that are automatically bootstrapped. 要自动启动的组件列表。 Usually there's only one component in this list, the _root component_ of the application. 通常,在这个列表中只有一个组件,也就是应用的*根组件*。 Angular can launch with multiple bootstrap components, each with its own location in the host web page. Angular 也可以引导多个引导组件,它们每一个都在宿主页面中有自己的位置。 A bootstrap component is automatically added to `entryComponents`. 启动组件会自动添加到 `entryComponents` 中。 |
entryComponents
|
A list of components that can be dynamically loaded into the view.
那些可以动态加载进视图的组件列表。
By default, an Angular app always has at least one entry component, the root component, `AppComponent`. Its purpose is to serve as a point of entry into the app, that is, you bootstrap it to launch the app.
默认情况下,Angular 应用至少有一个入口组件,也就是根组件 `AppComponent`。
它用作进入该应用的入口点,也就是说你通过引导它来启动本应用。
Routed components are also _entry components_ because they need to be loaded dynamically.
The router creates them and drops them into the DOM near a ` |