Nest.js get injector instance

2019-03-20 15:22发布

I want to create an instance of a dynamically loaded class trough Nest.js dependency injection service.

In Angular I would use Injector.create, what would be the equivalent in Nest.js ?

1条回答
等我变得足够好
2楼-- · 2019-03-20 16:08

First of all you should get a ModuleRef which references current module, and then use its "get" method to get an instance.

@Injectable()
export class AppletService {
  files: FileService;

  constructor(
    private moduleRef: ModuleRef,
  ) { 
    this.files = moduleRef.get(FileService);
  }
}
查看更多
登录 后发表回答