参数装饰器修饰函数参数,一般应用场景配合方法装饰器一起,达到检查参数的目的。
参数装饰器表达式会在运行时当作函数被调用,传入下列3个参数:
下面通过例子具体查看。
根绝参数器找到返回的值,然后利用方法装饰器返回处理后的结果。
import "reflect-metadata";
const response: ParameterDecorator = (
target: Object,
propertyKey: string | symbol,
parameterIndex: number
) => {
Reflect.defineMetadata("response", parameterIndex, target, propertyKey);
};
const get: (path: string) => MethodDecorator = (path) => {
return (
target: Object,
propertyKey: string | symbol,
descriptor: PropertyDescriptor
) => {
let originMethod = descriptor.value;
let index = Reflect.getMetadata("response", target,propertyKey);
descriptor.value = function () {
arguments[index] = {'data':'返回值'}
let arr = []
for (const eindex in arguments) {
if (Object.prototype.hasOwnProperty.call(arguments, eindex)) {
const element = arguments[eindex];
arr.push(element)
}
}
originMethod(...arr)
};
};
};
class HttpReq {
@get("/getAllData")
getAllData(request: {}, @response res?:{}) {
console.log(res);
}
}
let http = new HttpReq()
http.getAllData({'data':'requestBody'})
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有