创建自定义MethodDecorator携带信息
Create custom MethodDecorator to carry information
如何创建一个自定义 MethodDecorator 来承载如下数据而不需要任何逻辑?
export class CustomController
{
@Get()
@Report("GetLoanDetail")
public async getLoans(){}
}
TIA
如果你想使用 @SetMetadata
(或围绕它的简单包装器),那么你只需要像
这样简单的东西
export const Report = (title: string) => SetMetadata('report:metadata', title)
或者您可以像这样使用自己的 decorator factory and use the Reflect
API:
export const Report = (title: string) => {
return (target, propertyKey, descriptor) => {
Reflect.defineMetadata('report:metadata', title, target, propertyKey)
}
}
现在使用 Nest 的 Reflector
,您可以使用 this.refleector.get('report:metadata', context.getHandler())
获取元数据
如何创建一个自定义 MethodDecorator 来承载如下数据而不需要任何逻辑?
export class CustomController
{
@Get()
@Report("GetLoanDetail")
public async getLoans(){}
}
TIA
如果你想使用 @SetMetadata
(或围绕它的简单包装器),那么你只需要像
export const Report = (title: string) => SetMetadata('report:metadata', title)
或者您可以像这样使用自己的 decorator factory and use the Reflect
API:
export const Report = (title: string) => {
return (target, propertyKey, descriptor) => {
Reflect.defineMetadata('report:metadata', title, target, propertyKey)
}
}
现在使用 Nest 的 Reflector
,您可以使用 this.refleector.get('report:metadata', context.getHandler())