Ник*_*рсу 2 javascript typescript angular-decorator angular
我已经开始学习如何在我的应用程序中实现 TypeScript 装饰器。所以我从setTimeout. 它是一个方法装饰器,它在一段时间后执行方法。
例如:
@Decorators.timeout()
public someMethod () {}
Run Code Online (Sandbox Code Playgroud)
这是我的实现:
export class Decorators {
public static timeout (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): any {
let originalMethod = descriptor.value;
let decArguments = arguments;
descriptor.value = function Timeout () {
setTimeout(() => {
originalMethod.apply(this, decArguments);
}, 2000);
};
return descriptor;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
提供的参数与调用目标的任何签名都不匹配
可能是什么问题呢?
您args的Timeout()函数中缺少您,您应该将它们传递args给原始方法:
descriptor.value = function Timeout (...args) {
setTimeout(() => {
originalMethod.apply(this, args);
}, 2000);
};
Run Code Online (Sandbox Code Playgroud)
然后你应该删除这一行,因为它没有做任何事情:
let decArguments = arguments;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
991 次 |
| 最近记录: |