直接从另一个模块导入nestjs模块,而不导入到AppModule
P粉615886660
P粉615886660 2023-09-05 20:55:36
[JavaScript讨论组]

我是 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中的所有模块?如果否,如何解决此错误?

P粉615886660
P粉615886660

全部回复(1)
P粉916760429

尝试在 example.module.ts 中导出 DBModule 并在 AppModule 中导入 ExamplesModule

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号