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

bookUnderstanding Dependency Injection

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

Dependency injection is a way to provide one part of your application with access to another.

Instead of creating objects manually, Nest.js automatically provides them when needed.

In previous chapters, you used a service inside a controller:

constructor(private usersService: UsersService) {}

Here is what is happening:

  • UsersService: is a dependency;
  • The controller does not create it manually;
  • Nest.js injects it automatically.

Without dependency injection, you would have to create the service yourself:

const usersService = new UsersService();

This approach makes code harder to manage and reuse.

With dependency injection, Nest.js handles this for you.

To make a class injectable, use:

@Injectable()

Then register it in the module. providers: tells Nest which services can be injected.

This system allows different parts of your application to work together without being tightly connected.

question mark

What does dependency injection do in Nest.js?

Виберіть правильну відповідь

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

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

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