如何找到所有装饰有某种装饰的物业? [英] How to find all properties decorated with a certain decoration?
问题描述
我想将装饰器添加到已使用Angular Input
装饰器装饰的属性中.
我需要这个,因为我想为特定的 Component
获得一些(装饰的) Input
属性.
我发现了非常相似的问题,但是有一个很大的区别(我需要属性而不是类,所以我不能在类上引入新变量作为建议的答案).
我想要实现的是:
导出类BaseComponent{@Input()id:数字;@Input()@isFilter id2:数字;@Input()@isFilter id3:数字;公共getDefaultFilter():对象{令o:部分< this>= {};//将具有装饰器isFilter的this(self)上的所有属性分配给o!返回o;//o应该是{id2:what_is_set,id3:what_is_set}}}
您可以使用 reflect-metadata
将修饰后的属性存储为元数据.例如.以下代码将存储用 @isFilter()
修饰的所有属性的名称.然后,您可以调用 getFilteredProperties(this)
获得仅包含修饰属性的 this
的浅表副本.
import'reflect-metadata';const metadataKey = Symbol('isFilter');函数isFilter():(目标:对象,propertyKey:字符串)=>空白 {返回registerProperty;}函数registerProperty(target:object,propertyKey:string):void {let属性:string [] = Reflect.getMetadata(metadataKey,target);如果(属性){properties.push(propertyKey);} 别的 {属性= [propertyKey];Reflect.defineMetadata(metadataKey,属性,目标);}}函数getFilteredProperties(origin:object):object {const属性:string [] = Reflect.getMetadata(metadataKey,origin);const result = {};properties.forEach(key => result [key] = origin [key]);返回结果;}
您可能还想看看装饰器的TypeScript文档.
更新:由于Plunker目前似乎存在一些技术难题,因此我利用这次机会设置了 Stackblitz 用于上面的代码.仍然可以在此处找到.
I would like to add decorator to property already decorated with Angular Input
decorator.
I need this, because I want to get some (those which are decorated) Input
properties for specific Component
.
I found very similar question here, but with one big difference (I need it for property not class, so I can not introduce new variable on class as accepted answer proposed).
What I want to achieve is:
export class BaseComponent
{
@Input() id: number;
@Input() @isFilter id2: number;
@Input() @isFilter id3: number;
public getDefaultFilter(): object
{
let o: Partial<this> = {};
//Assign all properties on this(self), which has decorator isFilter, to o!!
return o; //o should be {id2: whatever_is_set, id3: whatever_is_set}
}
}
You can use reflect-metadata
to store the decorated properties as metadata. E.g. the following code will store the names of all properties decorated with @isFilter()
. You can then call getFilteredProperties(this)
to get a shallow copy of this
containing only the decorated properties.
import 'reflect-metadata';
const metadataKey = Symbol('isFilter');
function isFilter(): (target: object, propertyKey: string) => void {
return registerProperty;
}
function registerProperty(target: object, propertyKey: string): void {
let properties: string[] = Reflect.getMetadata(metadataKey, target);
if (properties) {
properties.push(propertyKey);
} else {
properties = [propertyKey];
Reflect.defineMetadata(metadataKey, properties, target);
}
}
function getFilteredProperties(origin: object): object {
const properties: string[] = Reflect.getMetadata(metadataKey, origin);
const result = {};
properties.forEach(key => result[key] = origin[key]);
return result;
}
You might also want to take a look at the TypeScript documentation for decorators.
Update: Since Plunker appears to have some technical difficulties at the moment I used the opportunity to set up a Stackblitz for the code above. The old Plunker can still be found here.
这篇关于如何找到所有装饰有某种装饰的物业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!