angular-cn/aio/dist/generated/docs/guide/migration-module-with-providers.json

5 lines
8.4 KiB
JSON
Raw Permalink Normal View History

{
"id": "guide/migration-module-with-providers",
"title": "ModuleWithProviders Migration",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/migration-module-with-providers.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=\"modulewithproviders-migration\"><code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a></code> Migration<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-module-with-providers#modulewithproviders-migration\"><i class=\"material-icons\">link</i></a></h1>\n<h2 id=\"what-does-this-schematic-do\">What does this schematic do?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-module-with-providers#what-does-this-schematic-do\"><i class=\"material-icons\">link</i></a></h2>\n<p>Some Angular libraries, such as <code>@angular/router</code> and <code>@ngrx/store</code>, implement APIs that return a type called <code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a></code> (typically via a method named <code>forRoot()</code>).\nThis type represents an <code><a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a></code> along with additional providers.\nAngular version 9 deprecates use of <code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a></code> without an explicitly generic type, where the generic type refers to the type of the <code><a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a></code>.</p>\n<p>This schematic will add a generic type to any <code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a></code> usages that are missing the generic.\nIn the example below, the type of the <code><a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a></code> is <code>SomeModule</code>, so the schematic changes the type to be <code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a>&#x3C;SomeModule></code>.</p>\n<p><strong>Before</strong></p>\n<code-example language=\"ts\">\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({...})\nexport class MyModule {\n <a href=\"api/upgrade/static\" class=\"code-anchor\">static</a> forRoot(config: SomeConfig): <a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a> {\n return {\n ngModule: SomeModule,\n providers: [\n {provide: SomeConfig, useValue: config}\n ]\n };\n }\n}\n</code-example>\n<p><strong>After</strong></p>\n<code-example language=\"ts\">\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({...})\nexport class MyModule {\n <a href=\"api/upgrade/static\" class=\"code-anchor\">static</a> forRoot(config: SomeConfig): <a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a>&#x3C;SomeModule> {\n return {\n ngModule: SomeModule,\n providers: [\n {provide: SomeConfig, useValue: config }\n ]\n };\n }\n}\n</code-example>\n<p>In the rare case that the schematic can't determine the type of <code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a></code>, you may see the schematic print a TODO comment to update the code manually.</p>\n<h2 id=\"why-is-this-migration-necessary\">Why is this migration necessary?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-module-with-providers#why-is-this-migration-necessary\"><i class=\"material-icons\">link</i></a></h2>\n<p><code><a href=\"api/core/ModuleWithProviders\" class=\"code-anchor\">ModuleWithProviders</a></code> has had the generic type since Angular version 7, but it has been optional.\nThis has compiled because the <code>metadata.json</code> files contained all the metadata.\nWith Ivy, <code>metadata.json</code> files are n
}