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

时间:2020-12-22 23:15:50

标签: javascript typescript nestjs

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

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

我有这个权利吗,这是您使用来自不同模块的谨慎服务的方式,还是我遗漏了什么?我问的原因是:如果这是正确的,并且您必须直接引用其他类(例如,直接在控制器中引用 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 {}

1 个答案:

答案 0 :(得分:0)

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

中使用 App1Service
  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 并且想要在 App3Service 中使用 App1Service,您应该使用选项 2。导出 {{1} } 在 App3Service 中,它将自动对 App3Module 可用。这就是它的工作原理:

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