- "contents": "<article>\n <div class=\"breadcrumb-container\">\n <div class=\"breadcrumb\">\n <script type=\"application/ld+json\">\n {\n \"@context\": \"http://schema.org\",\n \"@type\": \"BreadcrumbList\",\n \"itemListElement\": [\n { \"@type\": \"ListItem\", \"position\": 1, \"item\": { \"@id\": \"https://angular.cn//api\", \"name\": \"API\" } },\n { \"@type\": \"ListItem\", \"position\": 2, \"item\": { \"@id\": \"https://angular.cn/api/core\", \"name\": \"@angular/core\" } },\n { \"@type\": \"ListItem\", \"position\": 3, \"item\": { \"@id\": \"https://angular.cn/api/core/Inject\", \"name\": \"Inject\" } }\n ]\n }\n </script>\n <a href=\"/api\">API</a> > <a href=\"api/core\">@angular/core</a>\n </div>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular-cn/edit/aio/packages/core/src/di/metadata.ts?message=docs(core)%3A%20请简述你的修改...#L79-L81\" aria-label=\"提供编辑建议\" title=\"提供编辑建议\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular-cn/tree/14.1.0-next.0/packages/core/src/di/metadata.ts#L79-L81\" aria-label=\"查看源码\" title=\"查看源码\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n <header class=\"api-header\">\n <h1 id=\"inject\">Inject<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/core/Inject#inject\"><i class=\"material-icons\">link</i></a></h1>\n <label class=\"api-type-label decorator\">decorator</label>\n </header>\n <aio-toc class=\"embedded\"></aio-toc>\n\n <div class=\"api-body\">\n <section class=\"short-description\">\n <p translation-result=\"on\">类构造函数中依赖项参数上的参数装饰器,用于指定依赖项的自定义提供者。</p><p translation-origin=\"off\">Parameter decorator on a dependency parameter of a class constructor\nthat specifies a custom provider of the dependency.</p>\n\n\n </section>\n<section class=\"decorator-overview\">\n <table class=\"is-full-width list-table option-overview\">\n <thead>\n <tr><th>选项</th><th>说明</th></tr>\n </thead>\n <tbody>\n <tr class=\"option\">\n <td>\n <a class=\"code-anchor\" href=\"api/core/Inject#token\">\n <code>token</code>\n </a>\n </td>\n <td>\n\n <p translation-result=\"on\">一个 <a href=\"guide/glossary#di-token\">DI 令牌</a>,映射到要注入的依赖项。</p><p translation-origin=\"off\">A <a href=\"guide/glossary#di-token\">DI token</a> that maps to the dependency to be injected.</p>\n\n\n </td>\n </tr>\n </tbody>\n</table>\n \n</section>\n<section class=\"see-also\">\n <h2 id=\"see-also\">参见<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/core/Inject#see-also\"><i class=\"material-icons\">link</i></a></h2>\n <ul>\n <li><p translation-result=\"on\"><a href=\"guide/dependency-injection\">“依赖注入指南”</a></p><p translation-origin=\"off\"><a href=\"guide/dependency-injection\">\"Dependency Injection Guide\"</a></p></li>\n </ul>\n</section>\n\n\n<section class=\"decorator-options\">\n <h2 id=\"options\">选项<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/core/Inject#options\"><i class=\"material-icons\">link</i></a></h2>\n \n <a id=\"token\"></a>\n <table class=\"is-full-width option-table\">\n <thead>\n <tr>\n <th>\n <div class=\"with-github-links\">\n <h3 id=\"token\">token<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/core/Inject#token\"><i class=\"material-icons\">link</i></a></h3>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular-cn/edit/aio/packages/core/src/di/metadata.ts?message=docs(core)%3A%20请简述你的修改...#L61-L67\" aria-label=\"提供编辑建议\" title=\"提供编辑建议\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular-cn/tree/14.1.0-next.0/packages/core/src/di/metadata.ts#L61-L67\" aria-label=\"查看源码\" title=\"查看源码\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n </th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>\n <p translation-result=\"on\">一个 <a href=\"guide/glossary#di-token\">DI 令牌</a>,映射到要注入的依赖项。</p><p translation-origin=\"off\">A <a href=\"guide/glossary#di-token\">DI token</a> that maps to the dependency to be injected.</p>\n\n\n </td>\n </tr>\n <tr>\n <td>\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box api-heading\">\n token: any\n </code-example>\n </td>\n </tr>\n <tr>\n <td>\n </td>\n </tr>\n </tbody>\n </table>\n \n</section>\n\n<section class=\"usage-notes\">\n <h2 id=\"usage-notes\">使用说明<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/core/Inject#usage-notes\"><i class=\"material-icons\">link</i></a></h2>\n <p translation-result=\"on\">下面的示例显示了一个类构造函数,该构造函数使用参数装饰器指定了依赖项的自定义提供者。</p><p translation-origin=\"off\">The following example shows a class constructor that specifies a\ncustom provider of a dependency using the parameter decorator.</p>\n\n<p translation-result=\"on\">如果有 <code>@<a href=\"api/core/Inject\" class=\"code-anchor\">Inject</a>()</code>,则注入器将参数的类型注解用作提供者。</p><p translation-origin=\"off\">When <code>@<a href=\"api/core/Inject\" class=\"code-anchor\">Inject</a>()</code> is not present, the injector uses the type annotation of the\nparameter as the provider.</p>\n\n<code-example path=\"core/di/ts/metadata_spec.ts\" region=\"InjectWithoutDecorator\">\nclass Engine {}\n\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nclass Car {\n constructor(public engine: Engine) {\n } // same as constructor(@<a href=\"api/core/Inject\" class=\"code-anchor\">Inject</a>(Engine) engine:Engine)\n}\n\nconst injector = Injector.create(\n {providers: [{provide: Engine, deps: []}, {provide: Car, deps: [Engine]}]});\nexpect(injector.get(Car).engine instanceof Engine).toBe(true);\n\n</code-example>\n\n</section></div>\n</article>\n\n<!-- links to this doc:\n - api/core\n - api/core/forwardRef\n - api/forms/FormBuilder\n - api/upgrade/UpgradeAdapter\n - errors/NG2003\n - guide/aot-metadata-errors\n - guide/dependency-injection-in-action\n - guide/dependency-injection-providers\n - guide/hierarchical-dependency-injection\n - guide/http\n - guide/lightweight-injection-tokens\n - guide/styleguide\n - guide/testing-services\n - guide/upgrade\n-->\n<!-- links from this doc:\n - /api\n - api/core\n - api/core/Inject#inject\n - api/core/Inject#options\n - api/core/Inject#see-also\n - api/core/Inject#token\n - api/core/Inject#usage-notes\n - api/core/Injectable\n - guide/dependency-injection\n - guide/glossary#di-token\n - https://github.com/angular/angular-cn/edit/aio/packages/core/src/di/metadata.ts?message=docs(core)%3A%20请简述你的修改...#L61-L67\n - https://github.com/angular/angular-cn/edit/aio/packages/core/src/di/metadata.ts?message=docs(core)%3A%20请简述你的修改...#L79-L81\n - https://github.com/angular/angular-cn/tree/14.1.0-next.0/packages/core/src/di/metadata.ts#L61-L67\n - https://github.com/angular/angular-cn/tree/14.1.0-next.0/packages/core/src/di/metadata.ts#L79-L81\n-->"
0 commit comments