我正在尝试将方法传递给打字稿装饰器。当我将其用作嵌入式箭头功能时,我能够这样做。但是我不能通过以下方式做到这一点:
function preProcessor(preFn:any) {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
descriptor.value = function (param:any) {
return preFn(param);
}
};
}
class C {
@preProcessor(this.capitalize)
methodA(str: any) {
console.log(str) }
capitalize = (x:any)=>{return x.toUpperCase()}
}
var c = new C()
console.log(c.methodA("samurai..."))
这有效:
@preProcessor((x:any)=>{return x.toUpperCase()})