typescript - Nestjs中的es模块解析
问题描述
我有两个带有服务和常量的 Nestjs 模块 AModule 和 BModule。
在 AModule -> AService 中,我像导入 ASchemaKeyimport { ASchemaKy } from '../const';
并在构造函数中使用它
import { ASchemaKey } from '../const';
export class AService {
constructor(@InjectModel(ASchemaKey) private model) {}
}
在 BModule -> BService 我在构造函数中导入 AService
export class BService {
constructor(private aService: AService) {}
}
并得到了错误Nest can't resolve dependencies of the UserService (?). Please make sure that the argument undefinedModel at index [0] is available in the AModule context
。
通过调试,我发现 BService 文件比 AService 文件执行得更早,并且 AService 依赖的所有导入(如 ASchemaKey)都是未定义的
它适用于所有可能从 AModule 导入的东西,比如
import { ASchemaKey } from '@aModule/const'
ASchemaKey 会因为文件执行的顺序而未定义
有可能以某种方式修复吗?
我的 tsconfig
{
"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/*"]
},
}
}
解决方案
推荐阅读
- c# - DataGridView 的 CellFormatting 问题
- java - com/ibm/icu/text/RuleBasedCollator 的 Spring NoClassDefFoundError
- javascript - ExtJS Datefield TypeError j[d] 不是函数
- c# - 输入不是有效的 Base64 字符串,因为它包含非 base 64 字符
- java - 我制作了一个 Java 程序但有重复问题
- node.js - npm 更新目录路径混淆
- javascript - Razor - 为 radioButtonFor 赋予“属性”
- c# - 使用 Hotmail SMTP C# 发送电子邮件在本地工作,但不在实时服务器上
- javascript - javascript 只工作一次,在第一次
- c++ - 如何让线程等待条件执行操作而不使用过多的 CPU 时间?