首页 > 解决方案 > 装饰器 - 在子函数中读取标识符

问题描述

每次调用装饰器时,我都会创建一个需要在子函数中读取的标识符,我该怎么做?

例子

import { v4 as uuidv4 } from 'uuid';

export const DecoratorTest = (): MethodDecorator => {
    return (target: Object, key: string | symbol, descriptor: PropertyDescriptor) => {
        const method = descriptor.value;

        descriptor.value = async function (...args: any[]) {
            let identifier = uuidv4(); // Identifier I want to read in child function
            return await method.apply(this, args);
        };

        return descriptor;
    };
};

我在 func1 调用装饰器

@DecoratorTest()
function func1(param1: number, param2: number) {
    func2(param1, param2);
}

如何获得在装饰器中创建的标识符?

function func2(param1: number, param2: number) {
    // READ identifier decorator
}

标签: node.jstypescriptnestjs

解决方案


推荐阅读