[英]angular + restangular + typescript decorators error
我正在使用:
這是我的代碼:
function plain(){
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
var originalMethod = descriptor.value; // save a reference to the original method
descriptor.value = function(...args: any[]) {
var result = originalMethod.apply(this, args);
return result.then(function stripRestangularAttributes(response){
return response.plain();
});
};
return descriptor;
};
}
export class XYZ {
@plain
public getSomethingBySomething(data: Data): restangular.IPromise<any> {
if (!entityKey && !period) {
return null;
}
return this.restangularElement.all("route1/route2").post(data);
}
}
我收到以下錯誤:
錯誤TS1241:作為表達式調用時,無法解析方法裝飾器的簽名。 提供的參數與呼叫目標的任何簽名都不匹配。
它被拋出@plain
行。
一些信息:
.post(data)
,返回promise 我想要:
.plain()
,因此我將在任何方法上使用@plain
,所有結果將自動得到plain()
ed 。 @plain
裝飾器的方法: @plain
.then(function stripRestangularAttributes(response){ return response.plain(); })
不幸的是,在上面的示例中,我無法理解打字稿所抱怨的是什么。
PS:我一直在閱讀TS裝飾器指南中給出的出色答案 。
根據規范,您的方法裝飾器不應返回函數:
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
嘗試以下方法:
function plain(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
var originalMethod = descriptor.value; // save a reference to the original method
descriptor.value = function(...args: any[]) {
var result = originalMethod.apply(this, args);
return result.then(function stripRestangularAttributes(response){
return response.plain();
});
};
return descriptor;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.