簡體   English   中英

從另一個模塊注入 nestjs 服務

[英]Inject nestjs service from another module

我有一個PlayersModule和一個ItemsModule

我想在ItemsService中使用PlayersService

當我通過注射添加它時:

import { Injectable } from '@nestjs/common';
import { InjectModel } from 'nestjs-typegoose';
import { ModelType, Ref } from 'typegoose';
import { Player } from './player.model';
import { Item } from '../items/item.model';
import { ItemsService } from '../items/items.service';

@Injectable()
export class PlayersService {
    constructor(
        @InjectModel(Player) private readonly playerModel: ModelType<Player>,
        private readonly itemsService: ItemsService){}

我收到這個嵌套錯誤:

[Nest] 11592 - 2018-8-13 11:42:17 [ExceptionHandler] Nest 無法解析 PlayersService (+, ?) 的依賴項。 請確保索引 [1] 處的參數在當前上下文中可用。

這兩個模塊都導入到app.module.ts中。 這兩項服務都在各自的模塊中單獨工作。

您必須在提供它的模塊中導出ItemsService

@Module({
  controllers: [ItemsController],
  providers: [ItemsService],
  exports: [ItemsService]
  ^^^^^^^^^^^^^^^^^^^^^^^
})
export class ItemsModule {}

然后在使用該服務的模塊中導入導出模塊

@Module({
  controllers: [PlayersController],
  providers: [PlayersService],
  imports: [ItemsModule]
  ^^^^^^^^^^^^^^^^^^^^^^
})
export class PlayersModule {}

⚠️ 不要將同一個提供者添加到多個模塊中。 導出提供者,導入模塊。 ⚠️

假設您想在我的 TaskModule 的控制器中使用來自 AuthModule 的 AuthService

為此,您需要從 AuthModule 導出 authService

@Module({
    imports: [
     ....
    ],
    providers: [AuthService],
    controllers: [AuthController],
    exports:[AuthService]
  })
export class AuthModule {}
  

然后在TaskModule中,你需要導入AuthModule(注意:導入AuthModule不是TaskModule中的AuthService)

@Module({
    imports:[
      AuthModule
    ],
    controllers: [TasksController],
    providers: [TasksService]
  })
export class TasksModule {}

現在您應該可以在 TaskController 中使用 DI

@Controller('tasks')
export class TasksController {
   constructor(private authService: AuthService) {}
   ...
}
  

我通過從傳遞導出服務的構造函數中的參數中刪除@Inject()解決了我的問題。

Kim Kern 回答了這個問題。 但我只想提醒閱讀此評論的人。 每當您收到此錯誤時,您應該按照以下步驟操作,這些步驟可以幫助您輕松找出卡住的位置:

  • 確保提供提供程序的模塊已導入。
  • 確保您正在使用的提供程序已導出。

例如,您有包含類別服務的類別模塊,發布模塊具有發布服務並且它具有類別服務作為依賴項:

@Module({
    controllers: [CategoryController],
    providers: [CategoryService],
    exports: [CategoryService] // Remember to export
  })
export class CategoryModule {}

@Module({
    imports: [CategoryModule], // Make sure you imported the module you are using
    controllers: [PostController],
    providers: [PostService]
  })
export class PostModule {}

不要忘記使用這個注解。 Nest 使用它來檢測單例類。 在 spring boot - Java 中,這個曾經被稱為 Bean。 閱讀更多:

@Injectable()  
export class PostService {
  constructor(private readonly categoryService: CategoryService // This will be auto injected by Nestjs Injector) {}
}

我相信你遇到了和我一樣的問題。 我的場景是需要使用彼此服務的 2 個同級自定義模塊(用戶、身份驗證)。 我使用循環DI來解決它。 請檢查此鏈接

讓我知道它是否解決了您的問題,也許我可以為您提供進一步的建議。

通過更改在我的提供程序的@Inject()) 中使用的常量字符串 (TOKEN) 的導入方式解決了我的問題...小心使用 index.ts 和 export * from module.ts,nest 不會解決依賴問題

根據 Kim Kern 現在的回答,我們應該只將注入服務添加到我們的服務中,而不需要任何裝飾器(@Inject() 不需要)。 之后它將正常工作。 那是我的錯誤,可能可以幫助其他人。

步驟 1. 導出您想要的文件 步驟 2. 導入整個模塊。

我最初犯了一個錯誤,將文件添加為提供者並添加了拋出錯誤的模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM