2017-07-04 10:58:20 -04:00
# NgModule API
#### Prerequisites
A basic understanding of the following concepts:
2018-03-03 08:06:01 -05:00
对下列概念有基本的理解:
2017-07-04 10:58:20 -04:00
* [Bootstrapping ](guide/bootstrapping ).
2018-03-03 08:06:01 -05:00
2017-07-04 10:58:20 -04:00
* [JavaScript Modules vs. NgModules ](guide/ngmodule-vs-jsmodule ).
< hr / >
## Purpose of `@NgModule`
At a high level, NgModules are a way to organize Angular apps
and they accomplish this through the metadata in the `@NgModule`
decorator. The metadata falls
into three categories:
* **Static:** Compiler configuration which tells the compiler about directive selectors and where in templates the directives should be applied through selector matching. This is configured via the `declarations` array.
2018-03-03 08:06:01 -05:00
2017-07-04 10:58:20 -04:00
* **Runtime:** Injector configuration via the `providers` array.
2018-03-03 08:06:01 -05:00
2017-07-04 10:58:20 -04:00
* **Composability/Grouping:** Bringing NgModules together and making them available via the `imports` and `exports` arrays.
2018-01-17 07:57:43 -05:00
```typescript
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
@NgModule ({
2018-01-17 07:57:43 -05:00
// Static, that is compiler configuration
declarations: [], // Configure the selectors
entryComponents: [], // Generate the host factory
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
// Runtime, or injector configuration
providers: [], // Runtime injector configuration
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
// Composability / Grouping
imports: [], // composing NgModules together
exports: [] // making NgModules available to other parts of the app
2017-07-04 10:58:20 -04:00
})
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
```
## `@NgModule` metadata
2018-01-17 07:57:43 -05:00
The following table summarizes the `@NgModule` metadata properties.
2017-07-04 10:58:20 -04:00
2018-03-07 01:35:57 -05:00
下面是`@NgModule`元数据中属性的汇总表:
2017-07-04 10:58:20 -04:00
< table >
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< th >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
Property
2018-03-03 08:06:01 -05:00
属性
2018-01-17 07:57:43 -05:00
< / th >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< th >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
Description
2018-03-03 08:06:01 -05:00
2018-03-07 01:35:57 -05:00
说明
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / th >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td style = "vertical-align: top" >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< code > declarations< / code >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A list of [declarable ](guide/ngmodule-faq#q-declarable ) classes,
(*components*, *directives* , and *pipes* ) that _belong to this module_ .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< ol >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< li >
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
When compiling a template, you need to determine a set of selectors which should be used for triggering their corresponding directives.
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
< / li >
< li >
The template is compiled within the context of an NgModule— the NgModule within which the template's component is declared— which determines the set of selectors using the following rules:
2018-01-17 07:57:43 -05:00
< ul >
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
< li >
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
All selectors of directives listed in `declarations` .
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
< / li >
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
< li >
All selectors of directives exported from imported NgModules.
2018-03-03 08:06:01 -05:00
2018-03-06 22:25:56 -05:00
< / li >
< / ul >
< / li >
2018-01-17 07:57:43 -05:00
< / ol >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Components, directives, and pipes must belong to _exactly_ one module.
The compiler emits an error if you try to declare the same class in more than one module.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Don't re-declare a class imported from another module.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td style = "vertical-align: top" >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< code > providers< / code >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A list of dependency-injection providers.
2017-07-04 10:58:20 -04:00
2018-03-07 01:35:57 -05:00
依赖注入提供商的列表。
2018-01-17 07:57:43 -05:00
Angular registers these providers with the NgModule's injector.
If it is the NgModule used for bootstrapping then it is the root injector.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
These services become available for injection into any component, directive, pipe or service which is a child of this injector.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A lazy-loaded module has its own injector which
is typically a child of the application root injector.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Components in external modules continue to receive the instance provided by their injectors.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
For more information on injector hierarchy and scoping, see [Providers ](guide/providers ).
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td style = "vertical-align: top" >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< code > imports< / code >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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` ).
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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` .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td style = "vertical-align: top" >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< code > exports< / code >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A list of declarations— *component*, *directive* , and *pipe* classes— that
an importing module can use.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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` .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Declarations are private by default.
If this module does _not_ export `UserComponent` , then only the components within _this_
module can use `UserComponent` .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A module can list another module among its `exports` , in which case
all of that module's public components, directives, and pipes are exported.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
[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` .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td style = "vertical-align: top" >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< code > bootstrap< / code >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A list of components that are automatically bootstrapped.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Usually there's only one component in this list, the _root component_ of the application.
2017-07-04 10:58:20 -04:00
2018-03-07 01:35:57 -05:00
通常,在这个列表中只有一个组件,也就是应用的*根组件*。
2018-01-17 07:57:43 -05:00
Angular can launch with multiple bootstrap components,
each with its own location in the host web page.
2017-07-04 10:58:20 -04:00
2018-03-07 01:35:57 -05:00
Angular也可以引导多个引导组件, 它们每一个都在宿主页面中有自己的位置。
2018-01-17 07:57:43 -05:00
A bootstrap component is automatically added to `entryComponents` .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< tr >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td style = "vertical-align: top" >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< code > entryComponents< / code >
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
A list of components that can be dynamically loaded into the view.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
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 `<router-outlet>` .
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
While the bootstrapped and routed components are _entry components_ ,
you don't have to add them to a module's `entryComponents` list,
as they are added implicitly.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Angular automatically adds components in the module's `bootstrap` and route definitions into the `entryComponents` list.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
That leaves only components bootstrapped using one of the imperative techniques, such as [`ViewComponentRef.createComponent()` ](https://angular.io/api/core/ViewContainerRef#createComponent ) as undiscoverable.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
Dynamic component loading is not common in most apps beyond the router. If you need to dynamically load components, you must add these components to the `entryComponents` list yourself.
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
For more information, see [Entry Components ](guide/entry-components ).
2017-07-04 10:58:20 -04:00
2018-03-03 08:06:01 -05:00
要了解更多,参见[入口组件](guide/entry-components)一章。
2018-01-17 07:57:43 -05:00
< / td >
2017-07-04 10:58:20 -04:00
2018-01-17 07:57:43 -05:00
< / tr >
2017-07-04 10:58:20 -04:00
< / table >
< hr / >
## More on NgModules
You may also be interested in the following:
2018-03-03 08:06:01 -05:00
2017-07-04 10:58:20 -04:00
* [Feature Modules ](guide/feature-modules ).
2018-03-03 08:06:01 -05:00
2017-07-04 10:58:20 -04:00
* [Entry Components ](guide/entry-components ).
2018-03-03 08:06:01 -05:00
2017-07-04 10:58:20 -04:00
* [Providers ](guide/providers ).
2018-03-03 08:06:01 -05:00
2018-01-17 07:57:43 -05:00
* [Types of Feature Modules ](guide/module-types ).