我正在使用reflect-metadata打字稿。我编写了自己的属性装饰器,它被称为Field. 如何获取由Field,修饰的任何类型的字段/属性列表。例如:我想从类中获取ProductID,ProductName字段及其元数据Product,如下所示/。
import 'reflect-metadata';
export const FIELD_METADATA_KEY = 'Field';
export interface FieldDecorator {
field?: string;
title?: string;
type?: string;
}
export function Field(field: FieldDecorator) {
return Reflect.metadata(FIELD_METADATA_KEY, field);
}
export class Product {
@Field({
title: 'Product Id'
})
ProductID: string;
@Field({
title: 'Product Name',
type: 'text'
})
ProductName: string;
UnitPrice: number; //not decorated
}
Run Code Online (Sandbox Code Playgroud)