this example有什么好的解决办法吗? ?
似乎如果我有很多像 photo.module.ts
这样的模块,我需要在每个功能模块中导入 DatabaseModule
。
尝试将其放入 app.module.ts
但没有帮助。也许有一些使用 forRoot
静态导入的解决方案?
最佳答案
正如文档中所说:
In this article, you'll learn how to create a DatabaseModule based on the TypeORM package from scratch using custom providers mechanism. As a consequence, this solution contains a lot of overhead that you can omit using ready to use and available out-of-the-box dedicated @nestjs/typeorm package.
所以最好使用@nestjs/typeorm
包,参见docs .
当您使用@nestjs/typeorm
包时,您必须为每个功能模块导入TypeOrmModule.forFeature([PhotoEntity])
。在每个功能模块中,您只想注册属于该功能的实体。这改进了封装。
您尝试在 AppModule
中注册一个模块,并想知道为什么其提供程序在功能模块中不可用。请注意,模块始终必须导入模块本身,以便它可以访问其导出的提供程序。唯一的异常(exception)是 global modules 。当您使用装饰器 @Global()
注释模块时,只要它至少导入一次(例如在您的 AppModule
中),它就会自动导入到所有模块中.
关于javascript - NestJS TypeORM 导入解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990627/