dzzzzzy / Nestjs-Learning

nestjs 学习教程 :books:,跟我一起学习 nest 框架~ :muscle:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`@Inject(AppService)` in demo/rest-api

HaveF opened this issue · comments

commented

Nestjs-Learning/demo/rest-api at master · dzzzzzy/Nestjs-Learning
中的这一段:

@Controller()
export class AppController {

    /**
     * 构造函数,用于注入这个类的依赖,注入类时,需要使用 @Inject() 修饰符,其参数是被注入的类的类名
     */
    constructor(
        @Inject(AppService) private readonly appService: AppService,
    ) { }

我看官方文档中没有使用@Inject(AppService)这句呀

是我理解错什么了吗

@HaveF @Inject() 可以看作是一个语法糖,可有可无,依赖注入时他使用的是 appService 的类型。

注入被 @Injectable() 修饰的类时,可以不使用 @Inject(),我这里是为了规范我的代码风格写上去的。

commented

明白了, 谢谢解释, 赞规范风格