【问题标题】:Using service from injected module - NestJS使用来自注入模块的服务 - NestJS
【发布时间】:2021-04-01 15:37:45
【问题描述】:

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

这是有效的 - 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 {}

【问题讨论】:

    标签: javascript typescript nestjs


    【解决方案1】:

    如果你想在另一个服务中使用某些服务,有不同的方法,取决于你的目标是什么。例如:您有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. 您将App3Service 导出到App3Module
    2. 你在App2Module中导入App3Module
    3. 你在App1Module中导入App2Module

    【讨论】:

    • 谢谢@BidzinaAivazashvili。我在 service.module 中导出服务,并在 AppModule 类中导入模块。所以,它正在工作......它只是让我在我的 AppModule 类中有用于导入的模块和服务的导入语句,并且在我的消费控制器中也有导入语句。你是说这样好吗?
    • @monkeydeus 当您说“我的消费控制器中的导入语句”时,您的意思是在您的控制器构造函数中进行依赖注入吗?
    猜你喜欢
    • 2020-07-08
    • 2022-08-03
    • 2019-01-20
    • 2019-12-26
    • 2020-12-11
    • 2020-09-09
    • 2019-04-25
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多