索引 [1] 处的模块的类型为 "undefined"。检查您的导入语句和模块的类型
The module at index [1] is of type "undefined". Check your import statements and the type of the module
我遇到了 NestJs 中循环依赖的问题。错误:
Scope [AppModule -> PlanModule -> TaskModule] +17ms
Error: Nest cannot create the UserModule instance.
The module at index [1] of the UserModule "imports" array is undefined.
我尝试了什么:
- 在构造函数的用户和任务文件中的解析器文件中使用 forwardRef:
@Inject(forwardRef(() => TaskService))
private readonly tasksService: TaskService
- 实现OnModuleInit接口:
onModuleInit() {
this.userService = this.moduleRef.get(UserService);
}
- 在没有 index.ts 文件的情况下使用导入:
import { TaskModule } from '../task/task.module';
user.module
@Module({
providers: [UserService, UserResolver],
exports: [UserService],
imports: [JournalModule, PlanModule, TaskModule],
})
export class UserModule {}
task.module
@Module({
providers: [TaskService, TaskResolver],
exports: [TaskService],
imports: [UserModule],
})
export class TaskModule {}
plan.module
@Module({
providers: [PlanService, PlanResolver, GoalResolver],
imports: [TaskModule, ProgramModule],
exports: [PlanService],
})
export class PlanModule {}
您的 UserModule
导入 TaskModule
而 TaskModule
导入 UserModule
,因此您需要为这些模块导入设置前向引用 连同 服务注入。它看起来像这样:
user.module.ts
@Module({
providers: [UserService, UserResolver],
exports: [UserService],
imports: [JournalModule, PlanModule, forwardref(() => TaskModule)],
})
export class UserModule {}
task.module.ts
@Module({
providers: [TaskService, TaskResolver],
exports: [TaskService],
imports: [forwardRef(() => UserModule)],
})
export class TaskModule {}
我遇到了 NestJs 中循环依赖的问题。错误:
Scope [AppModule -> PlanModule -> TaskModule] +17ms
Error: Nest cannot create the UserModule instance.
The module at index [1] of the UserModule "imports" array is undefined.
我尝试了什么:
- 在构造函数的用户和任务文件中的解析器文件中使用 forwardRef:
@Inject(forwardRef(() => TaskService))
private readonly tasksService: TaskService
- 实现OnModuleInit接口:
onModuleInit() {
this.userService = this.moduleRef.get(UserService);
}
- 在没有 index.ts 文件的情况下使用导入:
import { TaskModule } from '../task/task.module';
user.module
@Module({
providers: [UserService, UserResolver],
exports: [UserService],
imports: [JournalModule, PlanModule, TaskModule],
})
export class UserModule {}
task.module
@Module({
providers: [TaskService, TaskResolver],
exports: [TaskService],
imports: [UserModule],
})
export class TaskModule {}
plan.module
@Module({
providers: [PlanService, PlanResolver, GoalResolver],
imports: [TaskModule, ProgramModule],
exports: [PlanService],
})
export class PlanModule {}
您的 UserModule
导入 TaskModule
而 TaskModule
导入 UserModule
,因此您需要为这些模块导入设置前向引用 连同 服务注入。它看起来像这样:
user.module.ts
@Module({
providers: [UserService, UserResolver],
exports: [UserService],
imports: [JournalModule, PlanModule, forwardref(() => TaskModule)],
})
export class UserModule {}
task.module.ts
@Module({
providers: [TaskService, TaskResolver],
exports: [TaskService],
imports: [forwardRef(() => UserModule)],
})
export class TaskModule {}