angular-cn/aio/dist/generated/docs/guide/ngmodule-faq.json

5 lines
51 KiB
JSON
Raw Permalink Normal View History

{
"id": "guide/ngmodule-faq",
"title": "NgModule FAQ",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/ngmodule-faq.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=\"ngmodule-faq\">NgModule FAQ<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/ngmodule-faq#ngmodule-faq\"><i class=\"material-icons\">link</i></a></h1>\n<p>NgModules help organize an application into cohesive blocks of functionality.</p>\n<p>This page answers the questions many developers ask about NgModule design and implementation.</p>\n<h2 id=\"what-classes-should-i-add-to-the-declarations-array\">What classes should I add to the <code>declarations</code> array?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/ngmodule-faq#what-classes-should-i-add-to-the-declarations-array\"><i class=\"material-icons\">link</i></a></h2>\n<p>Add <a href=\"guide/bootstrapping#the-declarations-array\">declarable</a> classes—components, directives, and pipes—to a <code>declarations</code> list.</p>\n<p>Declare these classes in <em>exactly one</em> module of the application.\nDeclare them in a module if they belong to that particular module.</p>\n<a id=\"q-declarable\"></a>\n<h2 id=\"what-is-a-declarable\">What is a <em>declarable</em>?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/ngmodule-faq#what-is-a-declarable\"><i class=\"material-icons\">link</i></a></h2>\n<p>Declarables are the class types—components, directives, and pipes—that\nyou can add to a module's <code>declarations</code> list.\nThey're the only classes that you can add to <code>declarations</code>.</p>\n<h2 id=\"what-classes-should-i-not-add-to-declarations\">What classes should I <em>not</em> add to <code>declarations</code>?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/ngmodule-faq#what-classes-should-i-not-add-to-declarations\"><i class=\"material-icons\">link</i></a></h2>\n<p>Add only <a href=\"guide/bootstrapping#the-declarations-array\">declarable</a> classes to an NgModule's <code>declarations</code> list.</p>\n<p>Do <em>not</em> declare the following:</p>\n<ul>\n<li>\n<p>A class that's already declared in another module, whether an app module, @NgModule, or third-party module.</p>\n</li>\n<li>\n<p>An array of directives imported from another module.\nFor example, don't declare <code>FORMS_DIRECTIVES</code> from <code>@angular/forms</code> because the <code><a href=\"api/forms/FormsModule\" class=\"code-anchor\">FormsModule</a></code> already declares it.</p>\n</li>\n<li>\n<p>Module classes.</p>\n</li>\n<li>\n<p>Service classes.</p>\n</li>\n<li>\n<p>Non-Angular classes and objects, such as\nstrings, numbers, functions, entity models, configurations, business logic, and helper classes.</p>\n</li>\n</ul>\n<h2 id=\"why-list-the-same-component-in-multiple-ngmodule-properties\">Why list the same component in multiple <code><a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a></code> properties?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/ngmodule-faq#why-list-the-same-component-in-multiple-ngmodule-properties\"><i class=\"material-icons\">link</i></a></h2>\n<p><code>AppComponent</code> is often listed in both <code>declarations</code> and <code>bootstrap</code>.\nYou might see the same component listed in <code>declarations</code>, <code>exports</code>, and <code>entryComponents</code>.</p>\n<p>While that seems redundant, these properties have different functions.\nMembership in one list doesn't imply membership in another list.</p>\n<ul>\n<li><code>AppComponent</code> could be declared in this module but not bootstrapped.</li>\n<li><code>AppComponent</code> could be bootstrapped in this module but declared in a different feature module.</li>\n<li>A component could be imported
}