15

我想通过 Nest.js 依赖注入服务创建一个动态加载的类的实例。

在 Angular 中我会使用Injector.create,在 Nest.js 中的等价物是什么?

4

1 回答 1

19

首先你应该得到一个引用当前模块的 ModuleRef,然后使用它的“get”方法来获取一个实例。

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

  constructor(
    private moduleRef: ModuleRef,
  ) { 
    this.files = moduleRef.get(FileService);
  }
}
于 2018-10-03T11:29:40.020 回答