有两种方法可以做到这一点:
A)生命周期事件
使用生命周期事件(类似于Angular中的更改检测挂钩)来运行代码并注入所需的服务,例如:
服务
export class AppService implements onModuleInit { onModuleInit() { console.log(`Initialization...`); this.doStuff(); }}模组
export class ApplicationModule implements onModuleInit { constructor(private appService: AppService) { } onModuleInit() { console.log(`Initialization...`); this.appService.doStuff(); }}B)执行上下文
使用执行上下文访问main.ts中的任何服务:
async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); const appService = app.get(AppService);}


