簡體   English   中英

我在從 NestJS 上已導入的模塊導入服務時遇到問題

[英]I'm having trouble importing a service from an already imported module on NestJS

好的,我會盡量把我的問題說清楚。 到目前為止,我的模塊結構與此類似:

OrderProductUnit 包含模塊 OrderProductUnitStatus 和 OrderProductUnitNotes。

從 OrderProductUnitNotes 的服務中,我想導入 OrderProductUnitStatus 的服務。 我已經從 OrderProductUnitNotes 的服務中導入了 OrderProductUnitStatus 的存儲庫,它工作得很好。 但是當我嘗試導入服務時,NestJS 會拋出這個錯誤:

Nest can't resolve dependencies of the OrderProductUnitNotesService (OrderProductUnitNoteRepository, OrderProductUnitStatusRepository, ?). Please make sure that the argument dependency at index [2] is available in the OrderProductUnitsModule context.

我嘗試過明確導入 OrderProductUnitStatusService,但似乎沒有什么不同。 我試過使用@Inject('OrderProductUnitStatusService') 方法,但是當我嘗試這個嵌套似乎掛起並且沒有加載所有模塊時,使得 API 沒有響應,就好像它完全關閉了一樣。

我決定將 OrderProductUnitStatus 和 OrderProductUnitNotes 這兩個模塊分組到 OrderProductUnit 模塊中,因為它們是直接連接的,所以對我來說,Notes 和 Status 都是 OrderProductUnit 的子級非常有意義。 但我開始重新考慮這個決定,如果有人能告訴我這種方法是否是一個好的決定,我將不勝感激。

在這一點上,我真的不知道還能嘗試什么,所以歡迎提出任何建議。 另外,如果需要任何其他信息,請告訴我,因為模塊之間的關系可能有點混亂,因為它們是嵌套的。

我在 Discord 上回答了這個問題,但最好有信息,錯誤表明您的服務之間存在循環文件導入。 當 Nest 提到它找不到dependency項而不是注入令牌的名稱(通常是類名)時,這意味着 Typescript 無法正確解析 class。 移除這種循環依賴當然是最好的選擇,但如果不可能的話,你可以在構造函數中使用@Inject(forwardRef(() => InjectedClass))來懶惰地設置注入令牌以供 Nest 解析。

暫無
暫無

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

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