Nestjs Config 访问引导级别的配置 [英] Nestjs Config access to config in bootstrap level

查看:98
本文介绍了Nestjs Config 访问引导级别的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此文档,您将配置导入 AppModule.
我正在尝试访问我的 main.ts 文件中引导程序级别的配置.像这样:

According to this documentation you import your config in AppModule.
I'm trying to access to config in bootstrap level in my main.ts file. Something like this:

const app = await NestFactory.create(AppModule);
  if (config.get('swagger.enabled'))
  {
    initSwagger(app);
  }
  await app.listen(8080);

此时我无法访问配置的问题,只有其他模块才能访问这样的配置:

The problem that I don't have access to config in this point, only other moudle will get access to config like this:

@Injectable()
export class SomeService {
    constructor(private readonly httpService: HttpService,
                  private readonly config: ConfigService) {}
}

我的问题:如何在引导程序级别访问nestjs-config"

My question: How to access to 'nestjs-config' in bootstrap level

推荐答案

在你的 main.ts 你可以做 const config = app.get(ConfigService) 并有在创建服务器之后但在开始侦听端口之前访问您的 ConfigService.

In your main.ts you can do const config = app.get(ConfigService) and have access to your ConfigService after you have created your server, but before you start listening on the port.

这篇关于Nestjs Config 访问引导级别的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆