简体   繁体   English

Angular如何知道构造函数的类型是什么?

[英]How does Angular know what the types of a constructor are?

I've been trying to replicate the way Angular interprets the constructor on an injectable service. 我一直在尝试复制Angular在可注入服务上解释构造函数的方式。

function Injectable() {
  return function<T extends { new (...args: any[]): {} }>(con: T) {
    return class extends con {
      static __constructorClasses: any[] = [];

      constructor(...args: any[]) {
        super(...args);
        Foo.__constructorClasses = ???;
      }
    };
  };
}

How can I return a list of classes into the Foo.__constructorClasses 如何将类列表返回到Foo.__constructorClasses

class Bar {
  public hello = 'world';
}

@Injectable()
class Foo {
  constructor(private bar: Bar) {}
}

// I expect: Foo.__constructorClasses = [Bar]
const bar = new Foo.__constructorClasses[0]();
console.log(bar.hello) // 'world'

Am I going about this in the wrong way? 我会以错误的方式处理吗? My goal is to know (after converting to es2015+) what each of the classes are in the constructor. 我的目标是(转换为es2015 +之后)知道构造函数中的每个类。 So that I either have the class itself [Bar] or a string representing the class 'Bar' . 这样我就可以拥有类[Bar]或代表类'Bar'的字符串。 The string is also fine because I can use it to pull the Bar instance from a Proxy. 该字符串也很好,因为我可以使用它从代理中获取Bar实例。

Utilizing: 利用:

  • typescript: 3.5.3 打字稿:3.5.3
  • reflect-metadata: 0.1.13 反射元数据:0.1.13

Command to run: 运行命令:

tsc foo.ts --emitDecoratorMetadata --experimentalDecorators && node foo.js

foo.ts: foo.ts:

import 'reflect-metadata';

type Constructor<T> = { new (...args: any[]): T };

function Injectable() {
  return function _Injectable<T extends Constructor<{}>>(Base: T) {
    return class __Injectable extends Base {
      classes: any[] = Reflect.getMetadata(
        'design:paramtypes',
        __Injectable.prototype.constructor
      );

      constructor(...args: any[]) {
        super(...args);
      }
    }
  };
}

class Bar {
  label: string;

  constructor() {
    this.label = 'BarClass';
  }
}

@Injectable()
class Foo {
  constructor(private bar: Bar) {}
}

const bar = new Bar();
const foo = new Foo(bar);

//@ts-ignore // ts doesn't strictly know that classes exists on Foo
console.log(foo.classes); // [ [Function: Bar] ]
//@ts-ignore // ts doesn't strictly know that classes exists on Foo
console.log(new foo.classes[0]().label); // 'BarClass'

Most of the information was available here : https://www.typescriptlang.org/docs/handbook/decorators.html however I thought this quick example would more simply demonstrate how Typescript can emit the classes passed to a classes constructor. 大多数信息都可以在这里找到https ://www.typescriptlang.org/docs/handbook/decorators.html但是,我认为这个简单的示例将更简单地演示Typescript如何发出传递给类构造函数的类。

Further Reading: 进一步阅读:

https://codeburst.io/decorate-your-code-with-typescript-decorators-5be4a4ffecb4 https://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html https://blog.wizardsoftheweb.pro/typescript-decorators-reflection/ https://codeburst.io/decorate-your-code-with-typescript-decorators-5be4a4ffecb4 https://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2 .html https://blog.wizardsoftheweb.pro/typescript-decorators-reflection/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM