NestJS无法解析UsersModule的依赖项 [英] NestJS Cannot resolve dependencies of the UsersModule
问题描述
NestJS无法解析UsersModule的依赖项.错误:
Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.
NestJS Cannot resolve dependencies of the UsersModule. Error:
Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.
app.module.ts:
app.module.ts:
@Module({
imports: [
ConfigModule,
DatabaseModule,
GraphQLModule,
UsersModule,
],
providers: [
ErrorService,
],
exports: [
DatabaseModule,
ErrorService,
],
})
export class AppModule implements NestModule {}
users.module.ts:
users.module.ts:
@Module({
imports: [
DatabaseModule,
ErrorService,
],
providers: [
UsersService,
...usersProviders,
UserResolver,
],
})
export class UsersModule {
constructor(private readonly errorService: ErrorService) {}
}
问题是此ErrorService,但是例如数据库模块以类似的方式使用,并且可以正常工作而没有任何错误.我有点困惑)也许有人会帮忙.谢谢.
Problem is this ErrorService, but for instance Database module is used in similar way, and it works without any error. I'm little confused) Maybe somebody would help. Thank you.
推荐答案
ErrorService
未正确注入UsersModule
中.
它应该是:
- 在
UsersModule
的providers
- 在由
UsersModule
组成的一个模块
import
的exports
中
- In the
providers
ofUsersModule
- In the
exports
of one moduleimport
ed byUsersModule
否则,Nest将无法解决它.并将其添加到AppModule
的exports
中也不会使其全局可用.
Otherwise, Nest won't be able to resolve it. And adding it to the exports
of AppModule
doesn't make it globally available, either.
我可以看到三种解决方案:
I can see three solutions:
- 1-将
ErrorService
添加到UsersModule
的providers
中.但这似乎不是一个正确的方法,因为我认为/猜测您将在代码的许多其他部分重复使用此服务(对吗?).因此,更好: - 2-创建一个
ErrorsModule
,其中exports
ErrorService
,然后UsersModule
将import
ErrorsModule
并能够注入服务.
- 1 - Adding
ErrorService
to theproviders
ofUsersModule
. But it doesn't look a proper way, as I think/guess that you will reuse this service in many other parts of the code (right?). Thus, better: - 2 - Create a
ErrorsModule
whichexports
theErrorService
, thenUsersModule
willimport
ErrorsModule
and will be able to inject the service.
赞:
// errors.module.ts
@Module({
providers: [ ErrorService ],
exports: [ ErrorService ],
})
export class ErrorsModule {}
// users.module.ts
@Module({
imports: [ErrorsModule],
})
export class UsersModule {
constructor(private readonly errorService: ErrorService) {}
}
- 3-我的最爱,如果您的服务应该被重用(
LoggerService
是一个更好的名字吗?如果我的猜测是正确的话),请使用一个@Global
模块,您只需在您的计算机中import
主模块(AppModule
) - 3 - My favorite, if your service is supposed to be reused (would
LoggerService
be a better name? If my guess is correct), use a@Global
module that you'llimport
only once in your main module (AppModule
)
示例:
@Global()
@Module({
providers: [LoggerService],
exports: [LoggerService],
})
export class LoggerModule {
constructor(private readonly loggerService: LoggerService) { /* initialize logger, or whatever */ }
}
然后,您必须将LoggerModule
添加到AppModule
的imports
中,并且LoggerService
将可以在任何地方注射,而无需重新声明.
Then you must add LoggerModule
to the imports
of AppModule
, and LoggerService
will be injectable anywhere without having to re-declare it.
这篇关于NestJS无法解析UsersModule的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!