In scaffolding, you can execute nest g mo XXX
create modules. Modules created by scaffolding commands are automatically imported into the root module registry.
Note: All modules in the project need to be imported into the root module and registered before they can be used.
shared module
nest g res boy
、nest g res girl
If you want girl
to use boy
the module in the module, you need to export its service in boy
the module
import {
Module } from '@nestjs/common';
import {
BoyService } from './boy.service';
import {
BoyController } from './boy.controller';
@Module({
controllers: [BoyController],
providers: [BoyService],
exports: [BoyService], // 导出 boy service
})
export class BoyModule {
}
girl
Then register boy
the module in the module
import {
Module } from '@nestjs/common';
import {
GirlService } from './girl.service';
import {
GirlController } from './girl.controller';
import {
BoyModule } from 'src/boy/boy.module';
@Module({
controllers: [GirlController],
providers: [GirlService],
imports: [BoyModule], // 注册 boy 模块
})
export class GirlModule {
}
Now you can girl
inject boy
service into the controller and use it~
import {
Controller, Get } from '@nestjs/common';
import {
GirlService } from './girl.service';
import {
BoyService } from 'src/boy/boy.service';
@Controller('girl')
export class GirlController {
constructor(
private readonly girlService: GirlService,
private readonly boyService: BoyService, // 注入 boy service
) {
}
@Get()
findAll() {
const result = `${
this.boyService.findAll()} and ${
this.girlService.findAll()}`;
return result;
}
}
global module
You can @Global()
decorate boy
the module and export its service
import {
Global, Module } from '@nestjs/common';
import {
BoyService } from './boy.service';
import {
BoyController } from './boy.controller';
@Global() // 使用 @Global() 修饰 boy 模块
@Module({
controllers: [BoyController],
providers: [BoyService],
exports: [BoyService], // 导出 service
})
export class BoyModule {
}
Now you can 任意
inject boy
service into the controller and use it~
dynamic module
If you need to pass parameters to the module, you can use dynamic modules
Add a static method to the module to receive parameters. @Module()
The content of the configuration object can be moved to the return value of the static method
import {
DynamicModule, Module } from '@nestjs/common';
import {
BoyService } from './boy.service';
import {
BoyController } from './boy.controller';
@Module({
/* 配置对象的内容都可以移到静态方法的返回值中 */
})
export class BoyModule {
// 编写静态方法, 接收参数
static forRoot(option: string): DynamicModule {
return {
module: BoyModule,
controllers: [BoyController],
providers: [BoyService, {
provide: 'boy', useValue: option }],
exports: [BoyService, {
provide: 'boy', useValue: option }],
global: true, // 等效于 @Global(); 默认为 false
};
}
}
app
Register the dynamic module in the root module module and pass in parameters
import {
Module } from '@nestjs/common';
import {
AppController } from './app.controller';
import {
AppService } from './app.service';
import {
BoyModule } from './boy/boy.module';
import {
GirlModule } from './girl/girl.module';
@Module({
controllers: [AppController],
providers: [AppService],
imports: [BoyModule, GirlModule, BoyModule.forRoot('superman')], // 注册模块并传入参数
})
export class AppModule {
}
Now you can 任意
inject dynamic modules into the controller and use them~
import {
Controller, Get, Inject } from '@nestjs/common';
import {
GirlService } from './girl.service';
import {
BoyService } from 'src/boy/boy.service';
@Controller('girl')
export class GirlController {
constructor(
private readonly girlService: GirlService,
private readonly boyService: BoyService,
@Inject('boy') private readonly boy: string, // 注入动态模块
) {
}
@Get()
findAll() {
const {
boyService, girlService, boy } = this;
const result = `${
boyService.findAll()} + ${
girlService.findAll()} + ${
boy}`;
return result;
}
}