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'的裝飾器,但是你打算在裝飾器中使用它做什麼?根據具體情況,可以通過裝飾器獲取實例成員。 –