如何从方法装饰器访问类元数据

tmu*_*sch 5 node.js typescript reflect-metadata typescript-decorator

我有两个装饰师.类装饰器和方法装饰器.类装饰器定义了我想在方法装饰器中访问的元数据.

ClassDecorator:

function ClassDecorator(topic?: string): ClassDecorator {
    return (target) => {
        Reflect.defineMetadata('topic', topic, target);
        // I've also tried target.prototype instead of target
        return target;
    };
}
Run Code Online (Sandbox Code Playgroud)

MethodDecorator:

interface methodDecoratorOptions {
    cmd: string
}

function MethodDecorator(options: decoratorOptions) {
    return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
        // HERE IS MY PROBLEM
        console.log('metaData is: ', Reflect.getMetadata('topic', target));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的班级定义:

@ClassDecorator('auth')
export class LoginClass {

    @MethodDecorator({
        cmd: 'login'
    })
    myMethod() {
        console.log('METHOD CALLED');
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

MethodDecorator的以下行返回metaData is: undefined.为什么不定义?

console.log('metaData is: ', Reflect.getMetadata('topic', target));
Run Code Online (Sandbox Code Playgroud)

问题:

如何从MethodDecorator访问ClassDecorator定义的元数据?

Tit*_*mir 7

问题是装饰器执行的顺序.首先执行方法装饰器,然后执行类装饰器.这有意义,如果你考虑它,类装饰器需要完整的类来处理,创建类涉及创建方法并首先调用它们的装饰器.

一个简单的解决方法是让方法装饰器注册一个回调,然后在设置主题后由类装饰器调用它:

function ClassDecorator(topic?: string): ClassDecorator {
    return (target) => {
        Reflect.defineMetadata('topic', topic, target.prototype);
        let topicFns: Array<() => void> = Reflect.getMetadata("topicCallbacks", target.prototype);
        if (topicFns) {
            topicFns.forEach(fn => fn());
        }
        return target;
    };
}

interface methodDecoratorOptions {
    cmd: string
}

function MethodDecorator(options: methodDecoratorOptions) {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        let topicFns: Array<() => void> = Reflect.getMetadata("topicCallbacks", target);
        if (!topicFns) {
            Reflect.defineMetadata("topicCallbacks", topicFns = [], target);
        }
        topicFns.push(() => {
            console.log('metaData is: ', Reflect.getMetadata('topic', target));
        });
    }
}

@ClassDecorator('auth')
class LoginClass {

    @MethodDecorator({
        cmd: 'login'
    })
    myMethod() {
        console.log('METHOD CALLED');
    }
}
Run Code Online (Sandbox Code Playgroud)