TypeScript setTimeout() 函数的装饰器

3

我开始学习如何在我的应用程序中实现TypeScript装饰器。所以我从 setTimeout 开始。这是一个方法装饰器,它会在一段时间后执行该方法。

例如:

@Decorators.timeout()
 public someMethod () {}

这是我的实现:

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;

  }

}

我收到的错误信息是:

提供的参数与调用目标的任何签名不匹配

可能的问题是什么?

2个回答

8

在您的Timeout()函数中缺少args,您应该将这些args传递到原始方法中:

descriptor.value = function Timeout (...args) {
  setTimeout(() => {
    originalMethod.apply(this, args);
  }, 2000);
};

您应该删除此行,因为它没有实际作用:
let decArguments = arguments;

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接