mar*_*adm 3 dependencies decorator code-injection injectable nestjs
如果我在模块中将服务声明为提供者,但没有在服务中使用 @Injectable 装饰器,会发生什么情况?
//Module
module({
controller: [catController],
provider: [catService]
})
//Service
//@Injectable()
export class catService{
}
Run Code Online (Sandbox Code Playgroud)
我认为令牌将被注册但不会被使用,并且每次都会共享新实例。我对么?
@Injectable()是一个有趣的例子,因为它的作用是告诉 NestJS 这个类需要注入依赖项。从技术上讲,如果没有要注入的依赖项,则可以省略此装饰器,但存在与框架的其余部分不再统一的风险。否则,类本身将充当任何其他 NestJS 提供程序,并且仍然可以注入到其他控制器和提供程序中。