【问题标题】:How to use global variables in nest.js?如何在nestjs中使用全局变量?
【发布时间】:2021-05-23 06:05:56
【问题描述】:

以配置为例,Nest.js 文档提倡注册 Config Modules 并以依赖注入的方式注入到其他模块中。

好处很明显,依赖和代码也很清楚,但是如果我有一个nest.js项目需要在启动时调用配置信息呢?这实际上给我带来了麻烦。

我的想法是使用存储(实际上是闭包)来管理全局可能需要的所有变量、客户端链接对象、在启动时注册并在需要时引入。

当相应的变量以这种方式注册后,可以在任何地方引入。缺点是需要自己管理依赖。

有了上面demo的概念设计:https://github.com/sophons-space/nest-server

请大家帮我指正,我还是个菜鸟。

【问题讨论】:

  • 我不太确定这里的问题是什么。 NestJS 是 NodeJS 之上的一个框架,因此(大部分)在 Node 中有效的任何事情都可以在 Nest 中完成。您是否发现自己的方法有错误?

标签: node.js typescript inversion-of-control nestjs global


【解决方案1】:

你可以使用通用的 NodeJS 方法

global.SomeGlobalVariableName = 'SomeGlobalVariableValue';

console.log(SomeGlobalVariableName);

【讨论】:

    【解决方案2】:

    如果你想使用 Nest 流,它应该在 configuration 文件中定义

    // app.module.ts
    import configuration from './config/configuration';
    
    imports: [
    // first import as first initialization
      ConfigModule.forRoot({
        isGlobal: true, // to get access to it in every component
        load: [configuration],
      }),
    ]
    ...
    // configuration.ts
    export default (): any => {
      return {
        someGlobalConfigVariable: parseInt(process.env.PORT, 10) || 3000,
      };
    };
    
    

    【讨论】:

      【解决方案3】:

      处理这种情况的方式类似于 NestJS 库或集成通常处理配置的方式;使用基础模块上的方法。

      main.ts

      import { NestFactory } from '@nestjs/core';
      import { AppModule } from './app.module';
      
      async function bootstrap() {
        // Note the `configure`-method
        const app = await NestFactory.create(AppModule.configure({
          myConfig: 'value',
        });
        await app.listen(3000);
      }
      bootstrap();
      

      app.module.ts

      import { DynamicModule } from '@nestjs/common';
      
      
      export class AppModule {
        static configure(config): DynamicModule {
           return {
             module: AppModule,
             providers: [{ provide: 'CONFIG', useValue: config }],
             // ....
           }
        }
      }
      

      【讨论】:

      • 我收到Nest can't resolve dependencies of the EntryController (EntryService, ?). Please make sure that the argument CONFIG at index [1] is available in the EntryModule context. 的错误,我错过了什么?虽然使用这个答案有效,stackoverflow.com/a/55413562,但我不想创建一个新模块
      【解决方案4】:

      创建一个 global.service.ts 文件(在文件夹中,您可以将其命名为 utils 或其他名称)并将代码放在下面

      export class GlobalService{ 
         static globalVar: any; 
      }
      

      将值设置为 globalVar

      GlobalService.globalVar = 'some value';
      

      从 globalVar 中获取值

      console.log(GlobalService.globalVar);
      

      注意不要忘记在任何你想使用的地方导入 GlobalService。

      【讨论】:

        【解决方案5】:

        我使用的方法是在 yaml 文件中使用我的配置变量,然后使用配置包在我的 Nestjs 项目中的任何位置获取这些变量或对象。例如在 default.yml 文件中 key: value 然后在我想使用它的文件中

        import config from 'config';
        let value = config.get<string>('key');
        

        您可以从此 npmjs 链接 here 获取此 pkg

        【讨论】:

          猜你喜欢
          • 2012-06-14
          • 2018-08-26
          • 2012-12-31
          • 2013-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多