0
0
NestJSframework~5 mins

Injectable decorator in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Injectable() decorator in NestJS?
The <code>@Injectable()</code> decorator marks a class as a provider that can be managed by NestJS's dependency injection system. It allows the class to be injected into other classes as a dependency.
Click to reveal answer
beginner
Can a class without <code>@Injectable()</code> be injected as a provider in NestJS?
No. Without the <code>@Injectable()</code> decorator, NestJS does not recognize the class as a provider and will not inject it into other classes.
Click to reveal answer
beginner
How does @Injectable() help with managing dependencies in NestJS?
It tells NestJS to create and manage an instance of the class automatically. This way, you don't have to manually create instances or manage dependencies yourself.
Click to reveal answer
intermediate
Is it possible to add metadata or options inside the @Injectable() decorator?
Yes. You can pass options to @Injectable() to control the provider's scope, like making it singleton or request-scoped.
Click to reveal answer
beginner
What happens if you forget to add @Injectable() on a service you want to inject?
NestJS will throw a runtime error saying it cannot resolve the dependency because the class is not registered as a provider.
Click to reveal answer
What does the @Injectable() decorator do in NestJS?
ARegisters a middleware
BDefines a controller route
CCreates a database connection
DMarks a class as a provider for dependency injection
Can you inject a class without @Injectable() in NestJS?
AYes, always
BOnly if it is a controller
CNo, it must have <code>@Injectable()</code>
DOnly if it extends another class
Which of these is a valid use of @Injectable() options?
ASetting the provider scope to singleton
BDefining HTTP routes
CConnecting to a database
DStyling components
What error occurs if you inject a class without @Injectable()?
ACannot resolve dependency error
BSyntax error
CType error
DNo error
Where should you place the @Injectable() decorator?
AOn HTML templates
BOn classes you want to inject as providers
COn environment configuration files
DOn database schemas
Explain in your own words what the @Injectable() decorator does in NestJS and why it is important.
Think about how NestJS knows which classes to create and share automatically.
You got /4 concepts.
    Describe what happens if you try to inject a class without the @Injectable() decorator in NestJS.
    Consider what NestJS needs to know to inject a class.
    You got /4 concepts.