admin管理员组文章数量:1287577
I am using nest js for back-end. For this project i started to use NestJs, and in the documentation i found that in nestJs we can build Global Modules
. So my global module looks like this:
//Module
import {Global, Module} from "@nestjs/mon";
import {GlobalController} from "./global.controller";
import {GlobalService} from "./global.service";
@Global()
@Module({
imports:[],
exports:[GlobalService],
providers:[GlobalService],
controllers:[GlobalController]
})
export class GlobalModule {}
I am using nest js for back-end. For this project i started to use NestJs, and in the documentation i found that in nestJs we can build Global Modules
. So my global module looks like this:
//Module
import {Global, Module} from "@nestjs/mon";
import {GlobalController} from "./global.controller";
import {GlobalService} from "./global.service";
@Global()
@Module({
imports:[],
exports:[GlobalService],
providers:[GlobalService],
controllers:[GlobalController]
})
export class GlobalModule {}
//Controller
import { Controller, Get } from "@nestjs/mon";
import {GlobalService} from "./global.service";
@Controller()
export class GlobalController {
constructor(private readonly getGlobalData: GlobalService) {
}
@Get('global')
getGlobal(){
return this.getGlobalData.getGlobalData()
}
}
//Service
import { Injectable } from "@nestjs/mon";
@Injectable()
export class GlobalService {
private readonly global = [1,12,2]
getGlobalData(){
return this.global.map(d => d)
}
}
In my root module i registered my global module:
import { Module } from '@nestjs/mon';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import {GlobalModule} from "./globalModule/global.module";
@Module({
imports: [GlobalModule], //register global module
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Now the module is working, and when i go to ..../global
i get my data from global
array.
How i understand we can create global module to avoid repeating the same code in each module.
Question: According to my example how can i use my global module in different modules?
- Does this answer your question? Globle module without import not working nestjs – Liam Commented Nov 11, 2022 at 17:07
2 Answers
Reset to default 7If you want to use that GlobalModule on other module you must import that module on your root module like this:
import { Module } from '@nestjs/mon';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { OtherModule } from "./otherModule/other.module";
import { GlobalModule } from "./globalModule/global.module";
@Module({
imports: [GlobalModule, OtherModule], //Import the other module
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Then in your other module you will be able to use the GlobalService without importing the GlobalModule, this is very useful when you need to use a service inside a Guard easily.
if you want to use some service of high frequency (such as prisam,jwt,redis,bull et al), you can just :
- put these all service in a globalModule
- registed globalModule in root module
- use these services in any other module without import global module again and again.
本文标签: javascriptHow to use Global module in NestJsStack Overflow
版权声明:本文标题:javascript - How to use Global module in NestJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741288114a2370390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论