NestJS · Junior · Frage 24 von 75
Wie bekommt ein Controller eine Service-Instanz?
@Controller("users")
export class UsersController {
constructor(private readonly usersService: UsersService) {}
}- 1Per Konstruktor-Injektion, anhand des Parametertyps
- 2Er muss sie mit
new UsersService()erzeugen - 3Er importiert eine globale Variable aus
main.ts
Antwort zeigen
Richtige Antwort: Per Konstruktor-Injektion, anhand des Parametertyps
Erklärung
Der Service muss in diesem Modul ein Provider sein (oder von einem importierten Modul exportiert werden). Nest löst den ganzen Abhängigkeitsgraphen auf.