【发布时间】:2021-11-07 11:43:51
【问题描述】:
我有一个CheckModule,它使用常量CHECK_OPTIONS 接受forRoot 方法中的配置:
@Module({})
export class CheckModule {
static forRoot(options?: CheckOptions): DynamicModule {
return {
module: CheckModule,
providers: [
{
provide: CHECK_OPTIONS,
useValue: options,
},
CheckService,
],
exports: [CheckService],
};
}
}
我的CheckService 使用选项:
@Injectable()
export class CheckService {
...
constructor(@Inject(CHECK_OPTIONS) options: CheckOptions) {}
...
每当我调试应用程序时,一切正常。但是,一旦我为生产环境构建并在 Heroku 上提供它,就会出现错误。
# Production build
nx build $PROJECT_NAME --prod
# Serving the app
node dist/apps/worker/main.js
我收到一个错误:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the CheckService (?). Please make sure that the argument CHECK_OPTIONS at index [0] is available in the CheckModule context.
我在这里遗漏了什么吗?我有点不知所措...
【问题讨论】:
-
这个选项是 .json 文件吗?构建后它可能不包含在 dist 中。如果是这样,请检查您的 tsconfig 文件是否已配置为加载它们。
-
这些选项是可选的。用户可以像这样包含模块
CheckModule.forRoot({ <options here> })
标签: heroku dependency-injection nestjs