网站搜索

typescript - nestjs 中的 es 模块解析


我有两个 NestJS 模块 AModule 和 BModule,它们包含服务和常量。

在 AModule -> AService 中,我像这样导入 ASchemaKey:import { 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 无法解析 UserService 的依赖项 (?). 请确保在 AModule 上下文中索引 [0] 的参数 undefinedModel 可用。

通过调试我发现 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/*"]
    },
  }
}