直接从另一个模块导入nestjs模块,而不导入到AppModule
P粉615886660
P粉615886660 2023-09-05 20:55:36
[JavaScript讨论组]
<p>我是 NestJs 的新手,我正在创建一个名为 <code>example .module</code> 的模块并导入另一个名为 <code>DB.Module</code> 的模块,但是如果我没有在 <code>App.Module</code> 中导入 <code>DB.Module</code>。是否必须导入 <code>App.Module</code></p> 中的所有模块 <pre class="brush:php;toolbar:false;">[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 */ ] })</pre> <p><strong>文件:<code>example.module.ts</code></strong></p> <pre class="brush:php;toolbar:false;">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 {}</pre> <p><strong>文件:<code>DB.module.ts</code></strong></p> <pre class="brush:php;toolbar:false;">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 {}</pre> <p><strong>文件:<code>App.module.ts</code></strong></p> <pre class="brush:php;toolbar:false;">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 {}</pre> <p>问题:是否必须导入<code>App.module</code>中的所有模块?如果否,如何解决此错误?</p>
P粉615886660
P粉615886660

全部回复(1)
P粉916760429

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

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

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