How to implement a typescript decorator?是关于如何在打字稿中使用装饰器的一个很好的例子。如何将实例变量传入打印机装饰参数?
考虑下面的情况下,
class MyClass {
@enumerable(false)
get prop() {
return true;
}
@property({required: true}) //here pass constant is no issue
public startDateString:string;
@property({afterDate: this.startDateString}) //how to pass startDateString here?
public endDateString:string;
}
function enumerable(isEnumerable: boolean) {
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
descriptor.enumerable = isEnumerable;
return descriptor;
};
}
我什么都试过,但似乎我没有办法通过startDateString
到装饰的说法。 startDateString
可能是一个变量,一个函数和一个参考。
这可能是可能的,具体取决于_how_和_when_您需要使用传入的值。我知道你需要将'startDateString'的errrr ... _instance value_传递给应用到'endDateString'的装饰器,但是你打算在装饰器中使用它做什么?根据具体情况,可以通过装饰器获取实例成员。 –