{"id":529,"date":"2026-06-24T21:38:09","date_gmt":"2026-06-24T13:38:09","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=529"},"modified":"2026-06-24T21:38:09","modified_gmt":"2026-06-24T13:38:09","slug":"ioc%e5%9c%a8nest%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/06\/24\/ioc%e5%9c%a8nest%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8\/","title":{"rendered":"IoC\u5728Nest\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728xxx.controller.ts\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u90fd\u4f1a\u6709\u5982\u4e0b\u7684\u88c5\u9970\u5668\uff0c\u6bd4\u5982<code>app.controller.ts<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller()<br>export class AppController {<br> &nbsp;constructor(private readonly appService: AppService) {}<br>    \/\/ ......<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AppController<\/code>\u7c7b\u901a\u8fc7<strong>@Controller<\/strong>\u88c5\u9970\u5668\u6765\u88c5\u9970\uff0c\u8868\u793a\u5b83\u53ef\u4ee5\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\uff0c\u7531Nest\u5185\u90e8\u7684IoC\u5bb9\u5668\u63a5\u7ba1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u7740\u6bd4\u5982<code>app.service.ts<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Injectable()<br>export class AppService {<br>    \/\/......<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AppService<\/code>\u7c7b\u901a\u8fc7<strong>@Injectable<\/strong>\u8fdb\u884c\u88c5\u9970\uff0c\u8868\u793a\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u88ab\u6ce8\u5165\uff0c\u6ce8\u610f<strong>@Injectable<\/strong>\u4e0d\u5355\u662f\u53ef\u4ee5\u88c5\u9970<strong>service(\u670d\u52a1)<\/strong>\u7c7b\uff0c\u5305\u62ec<strong>\u8fc7\u6ee4\u5668(Filter)\u3001\u62e6\u622a\u5668(Intercepter)\u3001\u63d0\u4f9b\u8005(Provide)\u3001\u7f51\u5173(Gateway)<\/strong>\u7b49\u7b49\uff0c\u5728\u6ca1\u6709\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0cNest\u4e2d\u6240\u9700\u8981\u4f9d\u8d56\u6ce8\u5165\u7684\u6a21\u5757\u90fd\u53ef\u4ee5\u4f7f\u7528<strong>@Injectable<\/strong>\u8fdb\u884c\u6807\u8bb0\uff0c\u4ee5\u4fbf\u5728IoC\u5bb9\u5668\u4e2d\u8fdb\u884c\u6536\u96c6\u548c\u7ba1\u7406\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>\u4e3a\u4ec0\u4e48\u63a7\u5236\u5668\u662f\u5355\u72ec\u4f7f\u7528<code>@Controller<\/code>\u6765\u88c5\u9970\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u63a7\u5236\u5668(Controller)<\/strong>\u53ea\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\uff0c\u4e0d\u4f5c\u4e3a\u4f9d\u8d56\u5bf9\u8c61\u88ab\u5176\u4ed6\u5bf9\u8c61\u7ec4\u4ef6\u6ce8\u5165\uff0c\u6211\u4eec\u5176\u5b9e\u53ef\u4ee5<strong>\u628a\u63a7\u5236\u5668\u770b\u505a\u662f\u6d88\u8d39\u8005<\/strong>\uff0c\u800c<strong>\u670d\u52a1(Service)<\/strong>\u548c<strong>\u4e2d\u95f4\u4ef6(Middleware)<\/strong>\u5219<strong>\u662f\u63d0\u4f9b\u8005<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u7ec4\u4ef6\u4f1a\u5728AppModule\u4e2d\u8fdb\u884c\u5f15\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;AppService],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>@Module<\/strong>\u88c5\u9970\u5668\u5728Nest\u4e2d\u7528\u4e8e\u5b9a\u4e49\u6a21\u5757\uff0c\u8fd9\u4e9b\u6a21\u5757\u5305\u542b\u9700\u8981\u6ce8\u5165\u7684\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u63a7\u5236\u5668\u4ec5\u4ec5\u53ea\u80fd\u4f5c\u4e3a\u6d88\u8d39\u8005\u88ab\u6ce8\u5165<\/strong>\uff0c\u800c<strong>\u63d0\u4f9b\u8005(Providers)<\/strong>\uff0c\u6bd4\u5982\u670d\u52a1(Service)\uff0c\u65e2\u80fd\u4f5c\u4e3a\u4f9d\u8d56\u88ab\u6ce8\u5165\uff0c\u4e5f\u53ef\u4ee5\u6ce8\u5165\u5230\u5176\u4ed6\u4f9d\u8d56\u5bf9\u8c61\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\uff0c\u5728Nest\u4e2d\u5b9e\u73b0\u6a21\u5757\u5316\u7ba1\u7406\u975e\u5e38\u7b80\u5355\u3002<strong><code>imports<\/code>\u5c5e\u6027\u7528\u4e8e\u5f15\u5165\u5176\u4ed6\u6a21\u5757<\/strong>\uff0c\u8fd9\u6709\u52a9\u4e8e\u5b9e\u73b0\u529f\u80fd\u903b\u8f91\u7684\u5206\u7ec4\u548c\u91cd\u7528\u3002\u6bd4\u5982\u6211\u4eec\u4e4b\u524d\u7684<code>UserModule<\/code>\uff0c\u5728\u6211\u4eec\u4f7f\u7528\u4e86<code>nest g resource user<\/code>\u547d\u4ee4\u4e4b\u540e\uff0c\u5728AppModule\u4e2d\u5c31\u81ea\u52a8\u5f15\u5165\u4e86<code>UserModule<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;AppService],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728<strong>User\u6a21\u5757<\/strong>\u4e2d\uff0c\u9ed8\u8ba4\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;],<br> &nbsp;controllers: &#91;UserController],<br> &nbsp;providers: &#91;UserService],<br>})<br>export class UserModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>UserController\u7c7b<\/strong>\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller('user')<br>export class UserController {<br> &nbsp;constructor(private readonly userService: UserService) {}<br> &nbsp;@Get()<br> &nbsp;findAll(): string {<br> &nbsp; &nbsp;return this.userService.findAll();<br>  }<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>UserService\u7c7b<\/strong>\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Injectable()<br>export class UserService {<br> &nbsp;findAll(): string {<br> &nbsp; &nbsp;return 'This action returns all users';<br>  }<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c5e\u6027\u6ce8\u5165<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u65e0\u8bba\u662f<strong>AppController<\/strong>\uff0c\u8fd8\u662f<strong>UserController<\/strong>\u4e2d\uff0c\u90fd\u662f\u662f\u901a\u8fc7<strong>\u6784\u9020\u5668\u6ce8\u5165<\/strong>\u7684<strong>UserService<\/strong>\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u80fd\u901a\u8fc7<strong>\u5c5e\u6027\u6ce8\u5165<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller('user')<br>export class UserController {<br>+  @Inject(UserService)<br>+  private userService: UserService;<br>\u200b<br>  \/\/ constructor(private readonly userService: UserService) {}<br>  @Get()<br>  findAll(): string {<br> &nbsp;  console.log('user controller');<br> &nbsp;  return this.userService.findAll();<br>  }<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">provider \u6ce8\u5165<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u9762\u6211\u4eec\u5c31\u7b80\u5355\u63d0\u4e86\u4e00\u4e0bprovider\u662f\u4f5c\u4e3a\u63d0\u4f9b\u8005\uff0c\u5176\u5b9e\u63d0\u4f9b\u7684\u5c31\u662f\u53ef\u4ee5\u6ce8\u5165\u7684\u5185\u5bb9\uff0c\u6bd4\u5982\u6211\u4eec\u524d\u9762\u662f\u8fd9\u6837\u5199\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;AppService],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u610f\u601d\u5c31\u662fAppService\u53ef\u4ee5\u88ab\u6ce8\u5165\uff0c\u8fd9\u91cc<strong>\u5176\u5b9e\u662f\u4e00\u79cd\u7b80\u5199<\/strong>\uff0c\u5b8c\u6574\u7684\u5199\u6cd5\u5e94\u8be5\u662f\u4e0b\u9762\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: AppService,<br> &nbsp; &nbsp; &nbsp;useClass: AppService,<br> &nbsp;  },<br>  ],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u8fc7 <code>provide<\/code> \u6307\u5b9a <code>token<\/code>\uff0c\u901a\u8fc7 <code>useClass<\/code> \u6307\u5b9a\u5bf9\u8c61\u7684\u7c7b<\/strong>\uff0cNest \u4f1a\u81ea\u52a8\u5bf9\u5b83\u505a\u5b9e\u4f8b\u5316\u540e\u7528\u6765\u6ce8\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AppController \u7684<strong>\u6784\u9020\u5668\u91cc\u53c2\u6570<\/strong>\u91cc\u58f0\u660e\u4e86 <code>AppService<\/code> \u7684\u4f9d\u8d56\uff0c\u5c31\u4f1a\u81ea\u52a8\u6ce8\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller()<br>export class AppController {<br> &nbsp;constructor(private readonly appService: AppService) {}<br> &nbsp;<br> &nbsp;\/\/ ......<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e0d\u60f3\u7528\u6784\u9020\u5668\u6ce8\u5165\uff0c\u4e5f\u53ef\u4ee5<strong>\u5c5e\u6027\u6ce8\u5165<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller('admin')<br>export class AppController {<br> &nbsp;@Inject(AppService)<br> &nbsp;private appService: AppService;<br>    <br> &nbsp;\/\/ ......<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <code>@Inject<\/code> \u6307\u5b9a\u6ce8\u5165\u7684 <code>provider<\/code> \u7684 <code>token<\/code> \u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6784\u9020\u5668\u53c2\u6570\u91cc\u6307\u5b9a AppService \u7684\u4f9d\u8d56\u7684\u65f6\u5019\u4e0d\u662f\u6ca1\u6709\u6307\u5b9a token\u5417\uff1f\u90a3\u662f\u56e0\u4e3a\u6211\u4eec\u76f4\u63a5\u628a<strong>AppService \u8fd9\u4e2aclass\u672c\u8eab\u4f5c\u4e3a\u4e86token<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2atoken\u53ea\u662f\u4e00\u4e2a\u6807\u8bc6\uff0c\u56e0\u6b64\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a<strong>\u5b57\u7b26\u4e32<\/strong>\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'app_service',<br> &nbsp; &nbsp; &nbsp;useClass: AppService,<br> &nbsp;  },<br>  ],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e48\uff0c\u65e0\u8bba\u662f\u6784\u9020\u5668\u53c2\u6570\u6ce8\u5165\uff0c\u8fd8\u662f\u5c5e\u6027\u6ce8\u5165\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u5f15\u5165\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684token<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller()<br>export class AppController {<br> &nbsp;@Inject('app_service')<br> &nbsp;private appService: AppService;<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller()<br>export class AppController {<br> &nbsp;constructor(@Inject('app_service') private readonly appService: AppService) {}<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528<strong>\u5b57\u7b26\u4e32<\/strong>\u6216\u8005 <strong>class<\/strong> \u505a <code>token<\/code> \u7684 <code>provider<\/code>\uff0c\u90fd\u53ef\u4ee5\u6b63\u786e\u88ab\u6ce8\u5165\u5230\u76ee\u6807\u5bf9\u8c61\u3002\u7528 <strong>class<\/strong> \u505a <code>token<\/code> \u53ef\u4ee5\u7701\u53bb<code>@Inject<\/code>\uff0c\u6bd4\u8f83\u7b80\u4fbf\u800c\u5df2\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u76f4\u63a5\u6ce8\u5165\u503c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e2\u7136provider\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u4e00\u4e2aclass\uff0c\u90a3\u5176\u5b9e\u4e5f\u80fd\u6307\u5b9a\u4e00\u4e2a<strong>\u503c<\/strong>\uff0c\u6bd4\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'app_service',<br> &nbsp; &nbsp; &nbsp;useClass: AppService,<br> &nbsp;  },<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'car',<br> &nbsp; &nbsp; &nbsp;useValue: {<br> &nbsp; &nbsp; &nbsp; &nbsp;brand: 'BYD',<br> &nbsp; &nbsp; &nbsp; &nbsp;price: 100000,<br> &nbsp; &nbsp;  },<br> &nbsp;  },<br>  ],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e48\uff0c\u5728controller\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u6ce8\u5165\u8fd9\u4e2a<strong>\u2019car\u2018<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller()<br>export class AppController {<br> &nbsp;@Inject('app_service')<br> &nbsp;private appService: AppService;<br>\u200b<br> &nbsp;@Inject('car')<br> &nbsp;private car: { brand: string; price: number };<br>\u200b<br> &nbsp;@Get('car')<br> &nbsp;hello(): string {<br> &nbsp; &nbsp;return `Hello, I have a ${this.car.brand} car, it's price is ${this.car.price}`;<br>  }<br>}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u52a8\u6001\u6ce8\u5165\u503c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">provider \u7684\u503c\u53ef\u80fd\u662f\u52a8\u6001\u4ea7\u751f\u7684\uff0cNest \u4e5f\u540c\u6837\u652f\u6301\uff0c\u901a\u8fc7\u5de5\u5382\u7684\u65b9\u5f0f\uff0c\u5176\u5b9e\u4e5f\u5c31\u662f\u51fd\u6570\u7684\u65b9\u5f0f\u6ce8\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const createRandomFactory = () =&gt; Math.random();<br>\u200b<br>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'app_service',<br> &nbsp; &nbsp; &nbsp;useClass: AppService,<br> &nbsp;  },<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'car',<br> &nbsp; &nbsp; &nbsp;useValue: {<br> &nbsp; &nbsp; &nbsp; &nbsp;brand: 'BYD',<br> &nbsp; &nbsp; &nbsp; &nbsp;price: 100000,<br> &nbsp; &nbsp;  },<br> &nbsp;  },<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'random',<br> &nbsp; &nbsp; &nbsp;useFactory: createRandomFactory,<br> &nbsp;  },<br>  ],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\uff0c\u5728app.controller.ts\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u6ce8\u5165\u8fd9\u4e2a<code>random<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Controller()<br>export class AppController {<br>    \/\/ ......<br>\u200b<br> &nbsp;@Inject('random')<br> &nbsp;private random: number;<br>\u200b<br> &nbsp;@Get('random')<br> &nbsp;randomNum(): number {<br> &nbsp; &nbsp;return this.random;<br>  }<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u4e14\uff0cuseFactory\u65e2\u7136\u53ef\u4ee5\u662f\u51fd\u6570\uff0c\u90a3\u4e48\u5f53\u7136\u53ef\u4ee5\u7ed9\u4ed6\u4f20\u9012\u53c2\u6570\uff0c\u800c\u4e14\u6ce8\u610f\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u662f\u901a\u8fc7Nest\u5bb9\u5668\u5e2e\u6211\u4eec\u7ba1\u7406\u7684\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const createRandomFactory = (<br> &nbsp;car: { brand: string; price: number },<br> &nbsp;appService: AppService,<br>) =&gt; {<br> &nbsp;return {<br> &nbsp; &nbsp;random: Math.random(),<br> &nbsp; &nbsp;brand: car.brand,<br> &nbsp; &nbsp;hello: appService.getHello(),<br>  };<br>};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u8fd9\u4e2a<code>createRandomFactory<\/code>\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\u4e86\uff0c\u5176\u5b9e\u610f\u601d\u5f88\u660e\u663e\uff0c\u9700\u8981\u5c06\u5177\u4f53\u7684\u503c\u4f20\u5165\u5230\u65b9\u6cd5\u4e2d\u53bb\uff0c\u4f46\u662f\u503c\u4ece\u54ea\u91cc\u6765\uff1f\u4e0d\u9519\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5bb9\u5668\u7ed9\u6211\u4eec\u6ce8\u5165\u8fdb\u6765\uff0c\u56e0\u4e3a<code>car<\/code>\u548c<code>app_service<\/code>\u8fd9\u4e24\u4e2a\u4e4b\u524d\u6211\u4eec\u90fd\u662f\u6709\u6ce8\u518c\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u63a5\u4e0b\u6765\u7684\u5199\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const createRandomFactory = (<br> &nbsp;car: { brand: string; price: number },<br> &nbsp;appService: AppService,<br>) =&gt; {<br> &nbsp;return {<br> &nbsp; &nbsp;random: Math.random(),<br> &nbsp; &nbsp;brand: car.brand,<br> &nbsp; &nbsp;hello: appService.getHello(),<br>  };<br>};<br>\u200b<br>@Module({<br> &nbsp;imports: &#91;UserModule],<br> &nbsp;controllers: &#91;AppController],<br> &nbsp;providers: &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'app_service',<br> &nbsp; &nbsp; &nbsp;useClass: AppService,<br> &nbsp;  },<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'car',<br> &nbsp; &nbsp; &nbsp;useValue: {<br> &nbsp; &nbsp; &nbsp; &nbsp;brand: 'BYD',<br> &nbsp; &nbsp; &nbsp; &nbsp;price: 100000,<br> &nbsp; &nbsp;  },<br> &nbsp;  },<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;provide: 'random',<br> &nbsp; &nbsp; &nbsp;useFactory: createRandomFactory,<br> &nbsp; &nbsp; &nbsp;inject: &#91;'car', 'app_service'],<br> &nbsp;  },<br>  ],<br>})<br>export class AppModule {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <strong>inject<\/strong>\u6ce8\u5165\u4e86\u4e24\u4e2a<code>token<\/code>\uff0c<code>'car'<\/code>\u548c <code>'app_service'<\/code>\u8fd9\u4e24\u4e2a\u6211\u4eec\u5728\u4e4b\u524d\u90fd\u5df2\u7ecf\u6ce8\u518c\u5230\u4e86\u5bb9\u5668\u4e2d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728xxx.controller.ts\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u90fd..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,18,14],"tags":[43,45],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-nestjs","category-nodejs","category-back","tag-ioc","tag-nestjs"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/nodejs\/nestjs\/\" rel=\"category tag\">NestJS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/nodejs\/\" rel=\"category tag\">NodeJS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=529"}],"version-history":[{"count":2,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/529\/revisions\/531"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}