Typescript nestjs中的es模块解析
我有两个nestjs模块AModule和BModule以及服务和常量 在AModule->AService中,我导入ASchemaKey,比如从“../const”导入{ASchemaKy}代码> 并在构造函数中使用它Typescript nestjs中的es模块解析,typescript,nestjs,es6-modules,Typescript,Nestjs,Es6 Modules,我有两个nestjs模块AModule和BModule以及服务和常量 在AModule->AService中,我导入ASchemaKey,比如从“../const”导入{ASchemaKy} 并在构造函数中使用它 import { ASchemaKey } from '../const'; export class AService { constructor(@InjectModel(ASchemaKey) private model) {} } export class BServic
import { ASchemaKey } from '../const';
export class AService {
constructor(@InjectModel(ASchemaKey) private model) {}
}
export class BService {
constructor(private aService: AService) {}
}
在b模块->b服务中,我在构造函数中导入服务
import { ASchemaKey } from '../const';
export class AService {
constructor(@InjectModel(ASchemaKey) private model) {}
}
export class BService {
constructor(private aService: AService) {}
}
并得到错误Nest无法解析UserService(?)的依赖项。请确保索引[0]处的参数undefinedModel在AModule上下文中可用
通过调试,我发现BService文件的执行时间早于服务文件,并且服务依赖的所有导入(如ASchemaKey)都未定义
它被认为是所有可能从AModule导入的东西,比如
从“@aModule/const”导入{ASchemaKey}
并且由于文件的执行顺序,ASchemaKey将未定义
有可能以某种方式修复吗
我的tConfig
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2015",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"moduleResolution": "node",
"importHelpers": true,
"paths": {
"@user/*": ["src/user/*"],
"@auth/*": ["src/auth/*"],
"@payment/*": ["src/payment/*"],
"@config/*": ["src/config/*"],
"@settings/*": ["src/settings/*"],
"@common/*": ["src/common/*"]
},
}
}
老实说,听起来文件导入之间可能存在循环依赖关系。是的,但这是非常奇怪的行为。所有从“A”文件导入的内容都将是未定义的,因为“B”文件的执行时间早于A,而解释器对文件一无所知,因此将其标记为未定义,正如我所理解的,这就是当您具有循环依赖关系并且模块加载顺序错误时发生的情况。除去依赖循环,它将得到解决,循环依赖处于一个相当深刻和意外的位置。不幸的是,nestjs不像angular那样显示文件依赖关系。问题出在我导入的index.ts中。。。调试模块的源代码以找出崩溃的地方非常有趣(不是)=)感谢帮助=)