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:

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<T>(key), which returns instance for equivalent token.

Example:

moduleRef.get<UsersService>(UsersService)

It returns instance of UsersService component from current module.

results matching ""

    No results matching ""