获取分配给相关属性的属性修饰器属性



我发现我需要一种方法来利用这两个装饰器,我正在努力解决这个问题。

基本上我想做两件事:1.使1类上的属性包含从一种类型到另一种类型的dataName上的元数据2.在同一个类中有一个方法,该方法查看属性上的信息,以找出如何翻译它:

糟糕的例子:

class Data {
@dataMap({word:'cheese', ignore:true}) //filter out
@dataMap({word:'item', ignore:false}) //filter in
public mapper[]
map(){
// get dataMap info and filter against it
return filtered mapper array;
}
}
//setup
const m = new Data();
m.mapper = [];
m.mapper.push("this cheese is bad");
m.mapper.push("this item is good");
const j = m.map();

现在我知道这不是最好的例子,因为数组的过滤器可以完成这项工作,但这正成为一项艰巨的任务,并映射到系统的许多部分。

我发现我很难写一个属性装饰器,它结合了存储DataMap的元数据和允许设置属性(没有映射器的构造函数注入(。

请帮忙(谢谢(,Kelly

我想我找到了答案。。。

之前我使用Reflect.metadata(target, props)。相反,我应该使用Reflect.defineMetadata(metadataKey, props, target, propertyKey)

因此,在上面的例子中添加:


interface IOptions {
word: string;
ignore: boolean;
}
const mkey = Symbol('mySpecialProps');
//this is the property decorator
function dataMap(options: IOptions): PropertyDecoratorType {
return function(target: any, propertyKey: string): any {
Reflect.defineMetadata(mkey, options, target, propertyKey);
return target[propertyKey];
};
}
//this is how to extract that data
function getDataMap(target: any, propertyKey: string){
return Reflect.getMetadata(mkey, target, propertyKey);
}

之后,您将在map()方法中引用getDataMap(this, 'mapper')