使用装饰器获取字段列表

Get list of fields with decorator

我创建我的装饰器

export function Id(target: any, key: string) { ... }

把它放在田地里

Class Test {
     @Id public id: int;
}

如何使用装饰器 (@Id) 获取字段列表

使用目标然后在使用自定义中设置密钥 属性(例如:__ pros __)

function myDecorator(target: any, key: string) {

  if (!target.__pros__) {
    target.__pros__ = []
  }
  target.__pros__.push(key);

}

Class A {
     @myDecorator name: string;
}

const obj = new A();
console.log("field with decorator on : ", a["__pros__"]);

控制台输出:

["name"]