【问题标题】:is not Injectable decorator required in providers?提供者不需要 Injectable 装饰器吗?
【发布时间】:2022-11-14 02:35:04
【问题描述】:

我正在使用示例应用程序研究 NestJS。

https://github.com/nestjs/nest/tree/master/sample/12-graphql-schema-first

不过,我比较好奇的是,即使service没有injectable decorator,也可以注册为模块的provider,其他provider的构造函数也可以使用注册的provider而不用injectable decorator。

实际上,我在上面的示例中从 src/cats/cats.service.ts 中删除了可注入装饰器。但它工作正常。

即使没有 Injectable 装饰器,相同的对象也会传递给提供者的构造函数。

为什么需要注入式装饰器?

【问题讨论】:

    标签: nestjs injectable


    【解决方案1】:

    injectable 是允许在该类中注入提供程序。如果你没有任何东西可以注入,你就不需要那个装饰器。但我建议您始终使用它。

    有关此主题的详细信息,请阅读:https://github.com/nestjs/docs.nestjs.com/pull/2481

    【讨论】:

      【解决方案2】:

      这里的答案是:“这取决于”。

      如果提供者没有注入依赖项,那么从技术上讲,不,您不需要 @Injectable() 装饰器。装饰器在幕后所做的是强制 Typescript 发出构造函数参数元数据。 Nest 将在运行时读取该元数据以了解注入到提供程序中的内容。 This PR goes into more depth on the @Injectable() decorator itself

      如果我们有以下课程

      @Injectable()
      export class Foo {
        constructor(private readonly foo: string) {}
      }
      @Injectable()
      export class Bar {
        constructor(private readonly bar: string) {}
      }
      @Injectable()
      export class FooBar {
        constructor(private readonly foo: Foo, private readonly bar: Bar) {}
      }
      

      然后我们得到这样的编译输出

      var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
          var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
          if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
          else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
          return c > 3 && r && Object.defineProperty(target, key, r), r;
      };
      var __metadata = (this && this.__metadata) || function (k, v) {
          if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
      };
      let Foo = class Foo {
          constructor(foo) {
              this.foo = foo;
          }
      };
      Foo = __decorate([
          Injectable(),
          __metadata("design:paramtypes", [String])
      ], Foo);
      export { Foo };
      let Bar = class Bar {
          constructor(bar) {
              this.bar = bar;
          }
      };
      Bar = __decorate([
          Injectable(),
          __metadata("design:paramtypes", [String])
      ], Bar);
      export { Bar };
      let FooBar = class FooBar {
          constructor(foo, bar) {
              this.foo = foo;
              this.bar = bar;
          }
      };
      FooBar = __decorate([
          Injectable(),
          __metadata("design:paramtypes", [Foo, Bar])
      ], FooBar);
      export { FooBar };
      

      __metadata('design:paramtypes', []) 是 Nest 最终读取的内容,并与 DI 容器注入令牌匹配

      【讨论】:

        猜你喜欢
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-08
        • 2020-07-13
        • 1970-01-01
        • 2022-08-19
        • 2016-02-28
        相关资源
        最近更新 更多