Import nestjs module directly from another module without importing to AppModule
P粉615886660
P粉615886660 2023-09-05 20:55:36
0
1
435
<p>I'm new to NestJs and I'm creating a module called <code>example .module</code> and importing another module called <code>DB.Module</code> but If I don't import <code>DB.Module</code> in <code>App.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

reply all(1)
P粉916760429

Try exporting DBModule in example.module.ts and import ExamplesModule in AppModule.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template