如何在 NestJS 中跨模块全局注入值? [英] How to globally inject value across modules in NestJS?

查看:494
本文介绍了如何在 NestJS 中跨模块全局注入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nx 工作区和 nestjs.我想在 nestjs 应用程序的多个模块中注入一个值.

最终目标是重现与 vsavkin 提到的 Angular 类似的配置管理方式

但似乎不可能,或者我错过了一些东西.

<块引用>

Nest 无法解析 FeatureService (?) 的依赖关系.请做出来确保索引 [0] 处的参数在 FeatureModule 中可用上下文.

我如何通知 FeatureModule 它需要访问这个全局注入值?

这在 AppService(根模块中的服务)中工作正常,但在任何子模块中都没有.

下面是我的代码.或者关于 codesandbox.io

的完整示例

app.module.ts

@Module({进口:[功能模块],控制器: [应用控制器],供应商: [应用服务,{提供:'我的令牌',useValue: '我的注入值',}],})导出类 AppModule {}

feature.module.ts

@Module({进口:[],控制器:[],供应商: [特色服务],})导出类 FeatureModule {}

feature.service.ts

@Injectable()导出类 AppService {构造函数(@Inject('MY-TOKEN') 私有注入值:字符串) {}}

解决方案

引自 NestJS 官方文档:

<块引用>

在 Angular 中,提供者在全局范围内注册.一旦定义,它们就随处可用.另一方面,Nest 将提供者封装在模块范围内.如果不导入模块提供程序,您将无法在其他地方使用它们.但有时,您可能只想提供一组应该始终可用的东西 - 开箱即用,例如:帮助程序、数据库连接等等.这就是您能够使模块成为全局模块的原因.

所以你可以做的只是定义一个带有 MY-TOKEN 提供者的全局模块:

<预><代码>@全球的()@模块({供应商: [{提供:'我的令牌',useValue: '我的注入值',}],出口:['我的令牌'],})导出类 GlobalModule {}

然后您可以使用其导出的值,而无需在任何地方导入全局模块.

I'm working with nx workspace and nestjs. I would like to inject a value across multiple modules in nestjs app.

Final goal is to reproduce similar way of configuration management as vsavkin mentioned for Angular

But it seems it's not possible, or I missed something.

Nest can't resolve dependencies of the FeatureService (?). Please make sure that the argument at index [0] is available in the FeatureModule context.

How can I notify FeatureModule it needs to access to this global injected value ?

This is working fine inside AppService (service in root module), but not in any sub modules.

Here is my code below. Or an full example on codesandbox.io

app.module.ts

@Module({
  imports: [
    FeatureModule
  ],
  controllers: [
    AppController
  ],
  providers: [
    AppService,
    {
      provide: 'MY-TOKEN',
      useValue: 'my-injected-value',
    }
  ],
})
export class AppModule {}

feature.module.ts

@Module({
  imports: [],
  controllers: [],
  providers: [
    FeatureService
  ],
})
export class FeatureModule {
}

feature.service.ts

@Injectable()
export class AppService {
  constructor(
    @Inject('MY-TOKEN') private injectedValue: string
  ) {}
}

解决方案

Quote from NestJS official documentation:

In Angular, the providers are registered in the global scope. Once defined, they're available everywhere. On the other hand, Nest encapsulates providers inside the module scope. You aren't able to use the module providers elsewhere without importing them. But sometimes, you may just want to provide a set of things which should be available always - out-of-the-box, for example: helpers, database connection, whatever. That's why you're able to make the module a global one.

So what you can do is just defining one global module with that MY-TOKEN provider:


@Global()
@Module({  
  providers: [
    {
      provide: 'MY-TOKEN',
      useValue: 'my-injected-value',
    }
  ],
  exports: ['MY-TOKEN'],
})
export class GlobalModule {}

and then you can use its exported values without importing the global module anywhere.

这篇关于如何在 NestJS 中跨模块全局注入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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