使用decorator获取参数值的Typescript

使用decorator获取参数值的Typescript,typescript,ecmascript-6,Typescript,Ecmascript 6,如何在装饰器中访问方法参数的值 export const NullParameterCheck = (target: Object, key: string, index: number) => { // how to get the value of the marked parameter in this case 'model' // ... do something with that value here. } 这就是我使用它的方式 public SetToolbar(@

如何在装饰器中访问方法参数的值

export const NullParameterCheck = (target: Object, key: string, index: number) => {
 // how to get the value of the marked parameter in this case 'model'
 // ... do something with that value here. 
}
这就是我使用它的方式

public SetToolbar(@NullParameterCheck model: ToolbarModel): void {
}
我所能找到的就是如何声明一个参数装饰器并记录它的每个参数
谢谢

声明类时调用装饰器,而不是调用方法时调用装饰器。您可以替换原始方法来截取和更改原始参数,但是您不能从参数装饰器替换方法,只能从方法装饰器替换方法,因此您需要将装饰器添加到函数中:

const NullParameterCheck = (index: number) => (target: any, key: string, propDesc: PropertyDescriptor) => {
    let originalFunction: Function = propDesc.value;
    propDesc.value = function () {
        let argValue = arguments[index];
        let newArgs = [];
        for (let i = 0; i < arguments.length; i++)newArgs.push(arguments[i]);
        newArgs[index] = argValue || {};

        return originalFunction.apply(this, newArgs);
    };
    return propDesc;
}

class ToolbarModel { }

class x {
    @NullParameterCheck(0)
    public SetToolbar( model: ToolbarModel): void {
        console.log(model);
    }
}

new x().SetToolbar(null); 
constnullparametercheck=(索引:number)=>(目标:any,键:string,propDesc:PropertyDescriptor)=>{
let originalFunction:Function=propDesc.value;
propDesc.value=函数(){
让argValue=arguments[index];
设newArgs=[];
对于(设i=0;i