
NestJS Modules & DI
Modular architecture, imports/exports, forRoot/forFeature, dynamic modules, circular dependencies
1What is a module in NestJS?
What is a module in NestJS?
回答
A module is a class annotated with the @Module() decorator that organizes code in a cohesive way. It encapsulates a set of providers, controllers, and other modules, allowing the application to be structured into modular components. Each NestJS application has at least one root module that serves as the entry point.
2What is the role of the 'providers' property in the @Module() decorator?
What is the role of the 'providers' property in the @Module() decorator?
回答
The providers property declares services and other classes that can be injected by the Dependency Injection system. These providers are instantiated by the NestJS container and can be shared within the module. They represent the business logic of the application and are accessible via dependency injection.
3How to make a provider available to other modules?
How to make a provider available to other modules?
回答
To make a provider accessible from other modules, it must be added to the exports array of the @Module() decorator. Only exported providers can be injected into other modules that import this module. This allows precise control over a module's public API and encapsulation of internal implementation.
What is the difference between 'imports' and 'exports' in a module?
What is Dependency Injection (DI) in NestJS?
+17 面接問題