angular-cn/aio/dist/generated/docs/guide/sharing-ngmodules.json

5 lines
6.0 KiB
JSON

{
"id": "guide/sharing-ngmodules",
"title": "Sharing modules",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/sharing-ngmodules.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"sharing-modules\">Sharing modules<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/sharing-ngmodules#sharing-modules\"><i class=\"material-icons\">link</i></a></h1>\n<p>Creating shared modules allows you to organize and streamline your code. You can put commonly\nused directives, pipes, and components into one module and then import just that module wherever\nyou need it in other parts of your app.</p>\n<p>Consider the following module from an imaginary app:</p>\n<code-example language=\"typescript\">\nimport { <a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a> } from '@angular/common';\nimport { <a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a> } from '@angular/core';\nimport { <a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a> } from '@angular/forms';\nimport { CustomerComponent } from './customer.component';\nimport { NewItemDirective } from './new-item.directive';\nimport { OrdersPipe } from './orders.pipe';\n\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({\n imports: [ <a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a> ],\n declarations: [ CustomerComponent, NewItemDirective, OrdersPipe ],\n exports: [ CustomerComponent, NewItemDirective, OrdersPipe,\n <a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a>, <a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a> ]\n})\nexport class SharedModule { }\n</code-example>\n<p>Note the following:</p>\n<ul>\n<li>It imports the <code><a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a></code> because the module's component needs common directives.</li>\n<li>It declares and exports the utility pipe, directive, and component classes.</li>\n<li>It re-exports the <code><a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a></code> and <code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code>.</li>\n</ul>\n<p>By re-exporting <code><a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a></code> and <code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code>, any other module that imports this\n<code>SharedModule</code>, gets access to directives like <code><a href=\"api/common/NgIf\" class=\"code-anchor\">NgIf</a></code> and <code>NgFor</code> from <code><a href=\"api/common/CommonModule\" class=\"code-anchor\">CommonModule</a></code>\nand can bind to component properties with <code>[(<a href=\"api/forms/NgModel\" class=\"code-anchor\">ngModel</a>)]</code>, a directive in the <code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code>.</p>\n<p>Even though the components declared by <code>SharedModule</code> might not bind\nwith <code>[(<a href=\"api/forms/NgModel\" class=\"code-anchor\">ngModel</a>)]</code> and there may be no need for <code>SharedModule</code>\nto import <code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code>, <code>SharedModule</code> can still export\n<code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code> without listing it among its <code>imports</code>. This\nway, you can give other modules access to <code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code> without\nhaving to import it directly into the <code>@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a></code> decorator.</p>\n<h3 id=\"using-components-vs-services-from-other-modules\">Using components vs services from other modules<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/sharing-ngmodules#using-components-vs-services-from-other-modules\"><i class=\"material-icons\">link</i></a></h3>\n<p>There is an important distinction between using another module's component and\nusing a service from another module. Import modules when you want to use\ndirectives, pipes, and components. Importing a module with services means that you will have a new instance of that service, which typically is not what you need (typically one wants to reuse an existing service). Use module imports to control service instantiation.</p>\n<p>The most common way to get a hold of shared services is through Angular\n<a href=\"guide/dependency-injection\">dependency injection</a>, rather than through the module system (importing a module will result in a new service instance, which is not a typical usage).</p>\n<p>To read about sharing services, see <a href=\"guide/providers\">Providers</a>.</p>\n<h2 id=\"more-on-ngmodules\">More on NgModules<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/sharing-ngmodules#more-on-ngmodules\"><i class=\"material-icons\">link</i></a></h2>\n<p>You may also be interested in the following:</p>\n<ul>\n<li><a href=\"guide/providers\">Providers</a>.</li>\n<li><a href=\"guide/module-types\">Types of Feature Modules</a>.</li>\n</ul>\n\n \n</div>\n\n<!-- links to this doc:\n - errors/NG0301\n - guide/module-types\n - guide/singleton-services\n-->\n<!-- links from this doc:\n - api/common/CommonModule\n - api/common/NgIf\n - api/core/NgModule\n - api/forms/FormsModule\n - api/forms/NgModel\n - guide/dependency-injection\n - guide/module-types\n - guide/providers\n - guide/sharing-ngmodules#more-on-ngmodules\n - guide/sharing-ngmodules#sharing-modules\n - guide/sharing-ngmodules#using-components-vs-services-from-other-modules\n - https://github.com/angular/angular/edit/master/aio/content/guide/sharing-ngmodules.md?message=docs%3A%20describe%20your%20change...\n-->"
}