0

考虑一个带有一个参数的类装饰器:

@TableName("Orders")
export class Order {
 // ...
}

装饰器定义为:

import "reflect-metadata";

const classDecoratorKey = Symbol.for("custom:TableName");

export function TableName(tableName: string): ClassDecorator {
  return (constructor: Function) => {
    Reflect.defineMetadata(classDecoratorKey, tableName, constructor);
  }
}

export function getTableName(target: any): string {
  return Reflect.getMetadata(classDecoratorKey, target.constructor, "class") || "";
}

我现在希望得到@TableName 值“Orders”。如何检索类装饰器的参数值?

let order = new Order();
getTableName(order); // "" but expected "Orders"
4

1 回答 1

0

这对我有用

export function TableName(tableName: string): ClassDecorator {
  return (constructor: Function) => {
    Reflect.defineMetadata(classDecoratorKey, tableName, constructor.prototype);
  };
}

export function getTableName(target: any): string {
  return Reflect.getMetadata(classDecoratorKey, target) || '';
}
于 2021-03-30T13:42:54.023 回答