我想从角度自定义装饰中的装饰方法中读取装饰参数值

时间:2018-02-06 23:25:08

标签: angular typescript angular2-decorators

以下是typescript中的装饰函数

export function URL(target: Object, propertyKey: string, parameterIndex: number) {
    let existingRequiredParameters: number[] = Reflect.getOwnMetadata(url, target, propertyKey) || [];
    existingRequiredParameters.push(parameterIndex);
    Reflect.defineMetadata(url, existingRequiredParameters, target, propertyKey);
}

export enum HttpMethodType {
    GET, POST, PUT
}



export function HttpMethod(type: HttpMethodType) {

    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
            let method = descriptor.value;
            descriptor.value = function(){

            //how do get the parameter that was decorated with @URL here in this code.
                    //let method = descriptor.value;
                    let urlParameters: number[] = Reflect.getOwnMetadata(url, target, propertyKey);

                    let urlParameterIndex;
                    if (!urlParameters || urlParameters.length === 0) {
                            throw new Error("Please have @URL annotation");
                    }
                    if (urlParameters.length > 1) {
                            throw new Error("Can not have multiple annotations for @req");
                    }
                    if (type === HttpMethodType.GET) {
                   //pass the parameter decorated with @url to http.get method         
                   let portResultsObservable = this.http.get(null, { withCredentials: true })

                    }
       // the arguments that i get is not matching the actual method that was decorated.
                    return method.apply(this, arguments);
            }

    }

}

我想调用我的方法装饰器和参数装饰器,如下所示。我想在HttpMethod装饰器函数中访问paramOne参数的值,我尝试使用HttpMethod传递..args []:any到内部函数,但我无法掌握这些参数。有什么方法可以访问这些参数吗?

@HttpMethod(HttpMethodType.GET)
callingfunction(@URL paramOne:string){
   console.log("test");
}

0 个答案:

没有答案