我是 NestJs 的新手,我正在创建一个名为 example .module 的模块并导入另一个名为 DB.Module 的模块,但是如果我没有在 App.Module 中导入 DB.Module。是否必须导入 App.Module
[Nest] 45706 - 07/19/2023, 7:47:55 PM LOG [NestFactory] Starting Nest application...
[Nest] 45706 - 07/19/2023, 7:47:55 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the DbexampleService (?, MysqlService). Please make sure that the argument MongoService at index [0] is available in the AppModule context.
Potential solutions:
- Is AppModule a valid NestJS module?
- If MongoService is a provider, is it part of the current AppModule?
- If MongoService is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing MongoService */ ]
})
文件:example.module.ts
import { Module } from '@nestjs/common';
import { DbexampleService } from './services/dbexample/dbexample.service';
import { HttpExampleService } from './services/http-example/http-example.service';
import { MongoService } from 'src/global/dbModule/services/mongo.service';
import { MysqlService } from 'src/global/dbModule/services/mysql.service';
import { DBModule } from '../global/dbModule/db.module';
@Module({
imports: [ DBModule],
providers:[DbexampleService, HttpExampleService, MongoService, MysqlService]
})
export class ExamplesModule {}
文件:DB.module.ts
import { Module } from '@nestjs/common';
import { MongoService } from './services/mongo.service';
import { DBController } from './controllers/db.controller';
import { MysqlService } from './services/mysql.service';
@Module({
controllers: [DBController],
providers: [MongoService, MysqlService],
exports:[MongoService, MysqlService]
})
export class DBModule {}
文件:App.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { configuration } from '../config/configuration';
import { DbexampleService } from './examples/services/dbexample/dbexample.service';
import { DbexampleController } from './examples/controllers/dbexample/dbexample.controller';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configuration]
})
],
controllers: [AppController, DbexampleController],
providers: [
AppService,
DbexampleService
],
})
export class AppModule {}
问题:是否必须导入App.module中的所有模块?如果否,如何解决此错误?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
尝试在
example.module.ts中导出DBModule并在AppModule中导入ExamplesModule。