在 nest.js 中导入一个具有动态模块作为依赖项的模块
Import a module which has a dynamic module as dependency in nest.js
我是 nest.js
的新手,我正在尝试在 APP 模块中使用来自 Q 模块的服务。
Q 模块正在为队列导入动态 BullModule
...
q.module.ts
import { BullModule } from '@nestjs/bull';
import { Module } from '@nestjs/common';
import { join } from 'path';
import { QService } from './q.service';
@Module({
imports: [
BullModule.registerQueue({
name: 'volume',
processors: [
{
name: 'create',
path: join(__dirname, 'vol.processor.js'),
},
],
}),
],
providers: [QService],
})
app.module.ts
const ENV = process.env.NODE_ENV;
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: !!ENV ? `.env.${ENV}` : '.env',
}),
BullModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
redis: {
host: configService.get('REDIS_HOST'),
port: Number(configService.get('REDIS_PORT')),
password: configService.get('REDIS_PASSWORD'),
},
}),
inject: [ConfigService],
}),
IdentityModule,
QModule,
],
controllers: [AppController],
providers: [
AppService,
IdentityService,
QService,
],
})
export class AppModule {}
Nest 说:
Nest can't resolve dependencies of the QService (?). Please make sure that the argument BullQueue_volume at index [0] is available in the AppModule context.
Potential solutions:
- If BullQueue_volume is a provider, is it part of the current AppModule?
- If BullQueue_volume is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing BullQueue_volume */ ]
})
我该如何处理?
我读了一些文档,但找不到 :(
而不是在 AppModule
的 providers
中重新定义 QService
,QModule
应该在 both[=28= 中有 QService
] providers
和 exports
。这将允许 imports: [QModule]
的任何模块使用 QModule
的导出而无需重新定义提供者。
附带说明一下,每次您将提供程序添加到模块的 providers
数组时,这将是该提供程序的一个新实例。在这种情况下,有两个实例,AppModule
想要访问 QService
需要的所有内容。这就是您应该使用 exports
的原因。
我是 nest.js
的新手,我正在尝试在 APP 模块中使用来自 Q 模块的服务。
Q 模块正在为队列导入动态 BullModule
...
q.module.ts
import { BullModule } from '@nestjs/bull';
import { Module } from '@nestjs/common';
import { join } from 'path';
import { QService } from './q.service';
@Module({
imports: [
BullModule.registerQueue({
name: 'volume',
processors: [
{
name: 'create',
path: join(__dirname, 'vol.processor.js'),
},
],
}),
],
providers: [QService],
})
app.module.ts
const ENV = process.env.NODE_ENV;
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: !!ENV ? `.env.${ENV}` : '.env',
}),
BullModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
redis: {
host: configService.get('REDIS_HOST'),
port: Number(configService.get('REDIS_PORT')),
password: configService.get('REDIS_PASSWORD'),
},
}),
inject: [ConfigService],
}),
IdentityModule,
QModule,
],
controllers: [AppController],
providers: [
AppService,
IdentityService,
QService,
],
})
export class AppModule {}
Nest 说:
Nest can't resolve dependencies of the QService (?). Please make sure that the argument BullQueue_volume at index [0] is available in the AppModule context.
Potential solutions:
- If BullQueue_volume is a provider, is it part of the current AppModule?
- If BullQueue_volume is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing BullQueue_volume */ ]
})
我该如何处理? 我读了一些文档,但找不到 :(
而不是在 AppModule
的 providers
中重新定义 QService
,QModule
应该在 both[=28= 中有 QService
] providers
和 exports
。这将允许 imports: [QModule]
的任何模块使用 QModule
的导出而无需重新定义提供者。
附带说明一下,每次您将提供程序添加到模块的 providers
数组时,这将是该提供程序的一个新实例。在这种情况下,有两个实例,AppModule
想要访问 QService
需要的所有内容。这就是您应该使用 exports
的原因。