【发布时间】:2019-02-06 18:38:56
【问题描述】:
如何使用DefaultValue 装饰器在我的TestClass 中更改foo 的值?
class TestClass {
@DefaultValue('bar')
private foo;
}
export const DefaultValue = (val: any): PropertyDecorator => {
return (target: any, propertyKey: string | symbol) => {
// Change the value of the decorated property
this[propertyKey] = val; // DOESN'T WORK!
};
}
不起作用:this[propertyKey] = val;
【问题讨论】:
-
Target 是持有该属性的类的原型(但我才刚刚开始学习 javascript/typescript...不是 100% 确定)。
标签: javascript typescript ecmascript-6 typescript-decorator