我有一个类装饰器(fooAnnotation)和方法装饰器(barDecorator)。
import 'reflect-metadata';
function fooAnnotation(value: string){
console.log('fooAnnotation init...');
return (target: any): void => {
console.log('fooAnnotation called...');
Reflect.defineMetadata('fooAnnotation', value, target);
};
};
function barAnnotation(value: string){
console.log('barAnnotation init...');
return (target: any, propertyKey: any): void => {
console.log('barAnnotation called...');
Reflect.defineMetadata('barAnnotation', value, target);
...
const fooAnnotationValue = Reflect.getMetadata('fooAnnotation', target);
// :( fooAnnotationValue is undefined!!!
};
};
@fooAnnotation('fooAnnotationValue')
class Foo{
@barAnnotation('barAnnotationValue')
public bar: string;
}
我的需要是 barAnnotation 必须使用 fooAnnotation 的值,但不幸的是 typescript 在方法装饰器之后评估类装饰器,因此在 barAnnotation 内部尚未定义 fooAnnotationMetadata。
方法装饰器如何检索类装饰器的值?