【发布时间】:2018-11-27 15:58:46
【问题描述】:
我正在尝试装饰一个 setter 函数,类似这样的类:
export class SearchResultSortBy{
private sortByChoice;
constructor() { ...}
/* getters & setters */
get sortBy() {
return this.sortByChoice;
};
@myDeco({o:'something',o2:'another something'})
set sortBy(sortBy) {
this.sortByChoice = sortBy;
};
}
与装饰器:
export function myDeco(element:any){
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
descriptor.set = function(...args: any[]) {
console.log("The method args are: " + JSON.stringify(args)); // pre
var result = originalMethod.apply(this, args); // run and store the result
someOtherStaticClass.someFunc(element); // post
return result; // return the result of the original method
};
return descriptor;
};
}
但是装饰器永远不会被调用/不做任何事情。
如果我将 @myDeco 放在 getter 函数的顶部,则调用装饰器并完成它的工作,这很奇怪......(装饰 setter 函数。)
是否可以在 typescript 中装饰 setter 函数?
【问题讨论】:
标签: typescript decorator