0

我创建了我的装饰器

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

把它放在田野上

Class Test {
     @Id public id: int;
}

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

4

1 回答 1

1

使用目标然后在使用自定义属性中设置键(例如:__ 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__"]);

控制台输出:

[“姓名”]

于 2018-01-09T02:41:06.020 回答