ModuleRef
优质
小牛编辑
134浏览
2023-12-01
ModuleRef
Sometimes you might want to directly get component instance from module reference. It not a big thing with Nest - just inject ModuleRef in your class:
有时候你可能想直接从模块引用中获取组件实例。这对于Nest来说很容易操作,你只需要在你的类中注入ModuleRef
即可:
import { ModuleRef } from '@nestjs/core';
import { Controller } from '@nestjs/common';
@Controller()
export class UsersController {
constructor(
private usersService: UsersService,
private moduleRef: ModuleRef) {}
}
ModuleRef provides one method:
- get(key), which returns instance for equivalent token.
ModuleRef
提供一个方法:
get<T>(key)
,该方法返回一个等价token
的实例:
Example:
示例
moduleRef.get<UsersService>(UsersService)
It returns instance of UsersService component from current module.
返回一个当前模块中的UsersService
组件。