繁体   English   中英

使用来自注入模块的服务 - NestJS

[英]Using service from injected module - NestJS

我正在尝试在 NestJS 的主脚手架应用 controller 中使用服务模块中的服务。

这是有效的 - helloWorldsService.message 在 @Get 方法中显示预期的问候 - 但是 app.module 和 app.controller 中 HelloWorldsService 的导入似乎是多余的,并且似乎违反了服务模块对服务的封装。

我有这个权利吗,这是您使用不同模块的谨慎服务的方式,还是我遗漏了什么? 我问的原因是:如果这是正确的,并且您必须直接引用其他类(例如,直接在控制器中引用 HelloWorldService),那么我很难理解为什么要打扰 @Module 声明的提供程序/导入属性。

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RouterModule } from './router/router.module';
import { ServicesModule } from './services/services.module'; //<-- import service MODULE
import { EventsModule } from './events/events.module';
import { HelloWorldsService } from './services/hello-worlds/hello-worlds.service'; //<-- import service module SERVICE


@Module({
  imports: [RouterModule, ServicesModule, EventsModule],
  controllers: [AppController],
  providers: [AppService, HelloWorldsService],
})
export class AppModule {}


//Controller code:
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { HelloWorldsService } from './services/hello-worlds/hello-worlds.service'; //<-- importing service again in consuming controller

@Controller()
export class AppController {
  constructor(private readonly appService: AppService, private readonly helloWorldsService: HelloWorldsService ) {}

    @Get()
    getHello(): string {
        return this.helloWorldsService.Message();
    }
}


//services.module
import { Module } from '@nestjs/common';
import { WagerAccountService } from './wager-account/wager-account.service';
import { WagerAccountHttpService } from './wager-account.http/wager-account.http.service';
import { CustomerIdentityHttpService } from './customer-identity.http/customer-identity.http.service';
import { HelloWorldsService } from './hello-worlds/hello-worlds.service';

@Module({
    exports:[HelloWorldsService],
  providers: [CustomerIdentityHttpService, WagerAccountService, WagerAccountHttpService, CustomerIdentityHttpService, HelloWorldsService]
})
export class ServicesModule {}

如果你想在另一个服务中使用一些服务,有不同的方法,取决于你的目标是什么。 例如:您有App1ServiceApp1ModuleApp2ServiceApp2Module并想在App1Service中使用App2Service

  1. 您可以使用providers
// App1Module.ts
@Module({
 imports: [],
 controllers: [App1Controller],
 providers: [App1Service, App2Service], // only service is imported here
})
export class App1Module {}
  1. 导入完整Module但不要忘记export服务
// App1Module.ts
@Module({
 imports: [App2Module], // Full Module is imported here
 controllers: [App1Controller],
 providers: [App1Service], // No need to use service import here but need to export App2Module services
})
export class App1Module {}

App2Service必须在App2Module中导出

// App2Module.ts
@Module({
  imports: [],
  controllers: [App2Controller],
  providers: [App2Service],
  exports: [App2Service] // this service is exported and available in other services
})
export class App2Module {}

如果您在App2Module中对其他模块有一些依赖关系,例如App3Module并希望在App1Service App3Service应该使用选项 2。在App3Module中导出App3Service ,它将自动可用于App1Service 这就是它的工作原理:

  1. 您在App3Module中导出App3Service
  2. 你在App2Module中导入App3Module
  3. 您在App1Module中导入App2Module

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM