Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Connects to the Database | Dependency Injection and Modules
Building Backend Applications with Nest.js

bookConnects to the Database

Свайпніть щоб показати меню

Dependency injection allows Nest.js to create and connect different parts of your application automatically.

When you use a service inside a controller, you don’t create it manually. Instead, Nest.js provides it for you.

For example:

constructor(private usersService: UsersService) {}

Here is what is happening:

  • UsersService: is requested by the controller;
  • Nest.js looks for it in the module;
  • The service is created and passed into the controller.

For this to work, the service must be registered:

@Module({
  providers: [UsersService],
})

The providers array tells Nest.js which classes can be injected.

Nest creates a single instance of the service and reuses it wherever needed. This avoids creating multiple copies and keeps your application consistent.

In simple terms, dependency injection connects parts of your application without requiring you to manage them manually.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 4. Розділ 2
some-alt