{"id":200726,"date":"2025-05-06T12:47:46","date_gmt":"2025-05-06T04:47:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200726\/"},"modified":"2025-05-06T12:47:46","modified_gmt":"2025-05-06T04:47:46","slug":"nest-%e4%b8%ad-typeorm-%e7%9a%84%e6%ad%a3%e7%a1%ae%e4%bd%bf%e7%94%a8%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0nest-cant-resolve-dependencies-of-the-bookservice","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200726\/","title":{"rendered":"Nest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can\\&#8217;t resolve dependencies of the BookService\u201d\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Nest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can\\&#8217;t resolve dependencies of the BookService\u201d\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u4e3b\u673a\u5b9d\u8d1d\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/173140688367332c23176a4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>nest \u4e2d typeorm \u7684\u6b63\u786e\u4f7f\u7528<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 nest \u65f6\uff0c\u51fa\u73b0\u9519\u8bef\u6d88\u606f\u201cnest can&#8217;t resolve dependencies of the bookservice\u201d\uff0c\u63d0\u793a bookentityrepository \u6ca1\u6709\u5728 appmodule \u4e2d\u6ce8\u518c\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>app.module.ts\uff1a<\/p>\n<pre>@module({\n    imports: [\n        typeormmodule.forroot({\n            \/\/ \u6570\u636e\u5e93\u914d\u7f6e\n        }),\n        bookmodule,\n    ]\n})\nexport class appmodule {}<\/pre>\n<p>book.module.ts\uff1a<\/p>\n<pre>@module({\n  imports: [typeormmodule.forfeature([bookentity])],\n  controllers: [bookcontroller],\n  providers: [bookservice],\n  exports: [bookservice]\n})\nexport class bookmodule {}<\/pre>\n<p>book.service.ts\uff1a<\/p>\n<pre>import { InjectRepository } from '@nestjs\/typeorm';\nimport { Repository } from 'typeorm';\n\nexport class BookService {\n    constructor(\n        @InjectRepository(BookEntity)\n        private readonly bookRepository: Repository&lt;BookEntity&gt;,\n    ) {}\n\n    \/\/ \u4e1a\u52a1\u903b\u8f91\n}<\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u5f0f<\/strong><\/p>\n<ol>\n<li>\u79fb\u9664 app.module.ts \u4e2d\u5197\u4f59\u7684 providers \u548c controllers \u4ee3\u7801\u3002<\/li>\n<li>\u786e\u4fdd bookentity \u5728 typeormmodule.forfeature \u4e2d\u88ab\u5f15\u7528\u3002<\/li>\n<\/ol>\n<p>\u5982\u6b64\u4fee\u6539\u540e\uff0cnest \u4fbf\u53ef\u4ee5\u6b63\u786e\u89e3\u6790 bookservice \u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u6d88\u9664\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aNest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can\\&#8217;t resolve dependencies of the BookService\u201d\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Nest \u4e2d TypeOrm \u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200726","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200726"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200726\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}