前言
模块指的是应用@Module
装璜器润饰的类,每个应用程序至多有一个模块,即根模块。根模块是Nest
用于构建应用程序的终点,实践上Nest
程序可能只有根模块,但在大多数状况下是存在多个模块的,每个模块各自封装一组相干的性能。
@Module装璜器
@Module()
装璜器能够传入一个对象,属性值如下:
providers |
将由 Nest 注入器实例化的提供程序,并且至多能够在该模块中共享 |
---|---|
controllers |
该模块中定义的必须实例化的控制器集 |
imports |
导入模块的列表,导出该模块所需的提供程序 |
exports |
该子集providers 由该模块提供,并且应该在导入该模块的其余模块中可用 |
@Module({
imports: [NanjiuModule, UserModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
模块共享
如果你想把以后模块的service
裸露给其它模块应用,则能够应用exports
到处该服务
比方我应用nest g resource info
新建了一个info类,并且应用export
导出该服务
// info.module.ts
import { Module } from '@nestjs/common';
import { InfoService } from './info.service';
import { InfoController } from './info.controller';
@Module({
controllers: [InfoController],
providers: [InfoService], // 提供者
exports: [InfoService] // 导出 InfoService 供其余模块应用
})
export class InfoModule {}
而后我在user
模块中应用imports
导入该模块
// user.module.ts
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { InfoModule } from 'src/info/info.module';
@Module({
imports: [InfoModule], // 导入 InfoModule
controllers: [UserController],
providers: [UserService]
})
export class UserModule {}
最初在controller
中依赖注入并应用
// user.controller.ts
import { InfoService } from 'src/info/info.service';
@Controller('user')
export class UserController {
constructor(
private readonly userService: UserService,
private readonly infoService: InfoService, // 注入 InfoService
) {}
@Post()
create(@Body() createUserDto: CreateUserDto) {
return this.infoService.findAll() // 调用 InfoService 的 findAll 办法
// return this.userService.create(createUserDto);
}
//...
}
这样就实现模块共享了,能够看到咱们在user
模块中能够调用info
的服务
模块再导出
能够把一些罕用的,公共的模块,全副先import进一个CommonModule,而后再把它们从exprots全副导出,当前如果有那个模块想要应用其中某个模块的Service,只须要将这个CommonModule导入即可,不必再导入所有的依赖模块
// common.module.ts
@Module({
imports: [Module1, Module2, Module3, Module4],
exports: [Module1, Module2, Module3, Module4],
})
export class CommonModule {}
依赖注入
模块类也能够注入provider
服务
@Module({
controllers: [UserController],
providers: [UserService],
})
export class UserModule {
constructor(private userService: UserService) {}
}
全局模块
通过@Global()
装璜器申明一个全局模块,只须要在根模块imports
注册该全局模块,就能够在其余所有模块内应用它导出的Service
比方:将info
申明为全局模块
// info.module.ts
@Global() // 全局模块
@Module({
controllers: [InfoController],
providers: [InfoService], // 提供者
exports: [InfoService] // 导出 InfoService 供其余模块应用
})
export class InfoModule {}
而后在user
模块中无需导入,只需依赖注入就可间接应用(前提是已在根模块导入)
// user.controller.ts
import { CreateUserDto } from './dto/create-user.dto';
import { InfoService } from 'src/info/info.service';
@Controller('user')
export class UserController {
constructor(
private readonly userService: UserService,
private readonly infoService: InfoService, // 注入 InfoService
) {}
@Post()
create(@Body() createUserDto: CreateUserDto) {
return this.infoService.findAll() // 调用 InfoService 的 findAll 办法
}
}
动静模块
动静模块可能让咱们创立可定制的模块,当导入模块并向其传入某些选项参数,这个模块依据这些选项参数来动静的创立不同个性的模块。
创立动静模块
动静模块其实就是给以后Module
类提供一个forRoot
办法,该办法返回一个新的Module
,这个Module的类型是一个DynamicModule,在其余模块须要注册应用时,能够应用xxxModule.forRoot(args)
来动静的注册不同的Module,以达到提供不同providers的目标。
这里咱们创立一个config
的动静模块
// config.module.ts
import { Module, DynamicModule, Global } from '@nestjs/common';
import { NanjiuService } from 'src/nanjiu/nanjiu.service';
import { UserService } from 'src/user/user.service';
interface Options {
name: string
}
@Global()
@Module({
})
export class ConfigModule {
static forRoot(options: Options): DynamicModule {
console.log('options', options)
return {
module: ConfigModule,
providers: [
{provide: 'config', useClass: options.name === 'nanjiu' ? NanjiuService : UserService},
],
exports: [
{provide: 'config', useClass: options.name === 'nanjiu' ? NanjiuService : UserService}
]
}
}
}
这个例子很简略,首先须要本人编写一个静态方法,该办法通过接管传递进来的参数判断应用哪一个service
,并且为了不便,我这里间接应用@Global()
装璜器将该模块申明称了全局模块
传递参数应用
调用静态方法传递参数
// app.module.ts
@Module({
imports: [ConfigModule.forRoot({name: 'fe'})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
而后在controller
中应用
import { Controller, Get, Inject } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
@Inject('config') private readonly configService // 注入 ConfigService
) {}
@Get('/hello2')
get2() {
return this.configService.getHello() // 调用 ConfigService 的 getHello 办法
}
}
比方下面forRoot
传递的参数是{name: 'nanjiu'}
,所以此时的ConfigModule
注入的应该是UserService
批改forRoot
参数
// app.module.ts
@Module({
imports: [ConfigModule.forRoot({name: 'nanjiu'})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
此时通过get
形式再拜访同样的路由,应该是拜访到NanjiuService
提供的服务了。
以上就是动静模块的简略用法,后续内容咱们还会再遇到它~
发表回复