代码之家  ›  专栏  ›  技术社区  ›  Julien METRAL

使用时,无法访问从共享模块提供的模块导出的服务ClientsModule.registerAsync()

  •  0
  • Julien METRAL  · 技术社区  · 5 年前

    在这个项目中,我使用的是nestjs微服务,我试图注册 ClientsModule 具有 registerAsync 方法访问我的auth模块中的auth config服务。

    /**
    config/  
      - shared-config.module.ts
      - app/
         - app-config.service.ts
         - app-config.module.ts
      - auth/
         - auth-config.service.ts
         - auth-config.module.ts
      ... other config modules
    - auth/
       - auth.module.ts
    - app/
       - app.module.ts
    */
    

    共享-配置模块.ts:

    @Module({
      imports: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */],
      export: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */]
    })
    export class SharedConfigModule {}
    

    @Module({
        imports: [
            ConfigModule.forRoot({
               ... some config
            }),
        ],
        providers: [MicroServiceAuthConfigService],
        exports: [MicroServiceAuthConfigService],
    })
    export class MicroServiceAuthConfigModule {}
    

    MicroServiceAuthConfigService 客户端模块 在我的 AuthModule

    认证模块.ts:

    @Module({
      imports: [
        SharedConfigModule,
        ClientsModule.registerAsync([
          {
            name: 'AUTH_PACKAGE',
            inject: [MicroServiceAuthConfigService],
            useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
              transport: Transport.GRPC,
              options: {
                url: authConfigService.url,
                package: authConfigService.package,
                protoPath: authConfigService.protoPath,
              },
            }),
          },
        ]),
      ],
      controllers: [AuthController],
      providers: [AuthService],
    })
    export class AuthModule {}
    

    @Module({
      imports: [AuthModule, SharedConfigModule],
    })
    export class AppModule {}
    

    因为我已经导入了SharedConfigModule,所以应该访问中的MicroServiceAuthConfigService useFactory 但我有以下错误:

    潜在解决方案:

    • 如果MicroServiceAuthConfigService是一个提供程序,它是当前ClientsModule的一部分吗?
    • 导入:[/*包含MicroServiceAuthConfigService的模块*/]})

    奇怪的是,我在我的应用程序模块中注入了 SharedConfigModule 在我的档案里 main.ts app.get(MicroServiceAuthConfigService) 而且很管用。

    1 回复  |  直到 5 年前
        1
  •  1
  •   Jay McDoniel    5 年前

    在你的 ClientsModule.registerAsync 你需要添加 imports 它的数组包含导出 MicroServiceAuthConfigService 供应商。看起来你特别需要

    @Module({
      imports: [
        SharedConfigModule,
        ClientsModule.registerAsync([
          {
            name: 'AUTH_PACKAGE',
            imports: [MicroServiceAuthConfigModule],
            inject: [MicroServiceAuthConfigService],
            useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
              transport: Transport.GRPC,
              options: {
                url: authConfigService.url,
                package: authConfigService.package,
                protoPath: authConfigService.protoPath,
              },
            }),
          },
        ]),
      ],
      controllers: [AuthController],
      providers: [AuthService],
    })
    export class AuthModule {}
    
    推荐文章