You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS中共享动态模块:多次注册动态模块会发生什么?

NestJS动态模块的模块级共享与重复注册行为
  • 核心诉求:希望在NestJS中实现模块层面的动态模块共享,而非仅通过全局作用域共享服务,重点关注多次注册动态模块时的行为表现。
  • 实践发现:多次注册动态模块时,NestJS会创建独立的模块实例,这一行为与Angular的模块机制不同,尽管NestJS常强调自身与Angular的相似性。
  • 现存困惑:已知module属性用于控制模块行为,但官方文档中未找到该场景下的详细说明,也缺少相关细节内容。

以下为示例思路代码(未验证):

import { DynamicModule, Module } from '@nestjs/common';
import { ConfigService } from './config.service';

@Module({})
export class ConfigModule {
  static register(options: Record<string, any>): DynamicModule {
    return {
      module: ConfigModule,
      providers: [
        {
          provide: 'CONFIG_OPTIONS',
          useValue: options,
        },
        ConfigService,
      ],
      exports: [ConfigService],
    };
  }
}

export const ConfiguredConfigModule = ConfigModule.register({key: 'flower'});
... 

@Module({
  imports: [ConfiguredConfigModule]
})
export class AppModule {}

内容的提问来源于stack exchange,提问作者Victor Shelepen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 16:52:06