Nest-Module 模块
Smoothness 2022/10/30  Nest
本文介绍Nest-Module,介绍了模块Service互用和全局模块使用、全局动态模块的定义和使用。
# 模块Service互用
- 场景:有两个模块
Bank模块和Module1模块,要在Module1模块中使用Bank模块的Service方法- 首先需要在
Bank模块中将Service导出import { Module } from '@nestjs/common'; import { BankController } from './bank.controller'; import { BankService } from './bank.service'; @Module({ imports: [], controllers: [BankController], providers: [BankService], exports: [BankService], }) export class BankModule {}1
2
3
4
5
6
7
8
9
10
11 - 然后将导出的
BankService导入到Module1模块中import { Module } from '@nestjs/common'; import { Module1Service } from './module1.service'; import { Module1Controller } from './module1.controller'; import { BankService } from '../Bank/bank.service'; @Module({ controllers: [Module1Controller], providers: [ Module1Service, { provide: 'BankService', useClass: BankService, }, ], }) export class Module1Module {}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 - 最后就可以在
Module1中使用import { Controller, Get, Inject } from '@nestjs/common'; import { BankService } from '../Bank/bank.service'; @Controller('module1') export class Module1Controller { constructor( @Inject('BankService') private readonly bankService: BankService, ) {} @Get() findAll() { return this.bankService.findAll(); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 
 - 首先需要在
 
# 全局模块使用
全局模块装饰器
@Global 是全局模块装饰器。
全局模块创建
import { DynamicModule, Global, Module } from '@nestjs/common'; @Global() @Module({}) export class GlobalModuleModule { static findPath(): DynamicModule { return { module: GlobalModuleModule, providers: [ { provide: 'GlobalModule', useValue: { base: '/111111111111', }, }, ], exports: [ { provide: 'GlobalModule', useValue: { base: '/111111111111', }, }, ], }; } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27全局模块挂载
import { Module } from '@nestjs/common'; import { GlobalModuleModule } from './global-module/global-module.module'; @Module({ imports: [ GlobalModuleModule.findPath(), ], }) export class AppModule {}1
2
3
4
5
6
7
8
9全局模块使用
import { Controller, Get, Inject } from '@nestjs/common'; @Controller('module1') export class Module1Controller { constructor( @Inject('GlobalModule') private readonly GlobalModule: any, ) {} @Get() findAll() { return this.GlobalModule; } }1
2
3
4
5
6
7
8
9
10
11
12
13
# 全局动态模块
全局动态模块
全局动态模块就是全局模块的有参数版本,参数在挂载的传入。
全局动态模块创建
import { DynamicModule, Global, Module } from '@nestjs/common'; interface IOptions { path: string; } @Global() @Module({}) export class GlobalModuleModule { static findPath(options: IOptions): DynamicModule { return { module: GlobalModuleModule, providers: [ { provide: 'GlobalModule', useValue: { base: '/111111111111' + options.path, }, }, ], exports: [ { provide: 'GlobalModule', useValue: { base: '/111111111111' + options.path, }, }, ], }; } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31全局动态模块挂载
import { Module } from '@nestjs/common'; import { GlobalModuleModule } from './global-module/global-module.module'; @Module({ imports: [ GlobalModuleModule.findPath({ path: '动态全局模块', }), ], }) export class AppModule {}1
2
3
4
5
6
7
8
9
10
11全局动态模块使用
import { Controller, Get, Inject } from '@nestjs/common'; @Controller('module1') export class Module1Controller { constructor( @Inject('GlobalModule') private readonly GlobalModule: any, ) {} @Get() findAll() { return this.GlobalModule; } }1
2
3
4
5
6
7
8
9
10
11
12
13