属性装饰器
- 参数
export default function (proto, key) {
// 两个参数
}
- 给属性增加metadata
import 'reflect-metadata';
export default function (label, type?) {
// 在装饰器内,增加元数据,能够自动加到属性上
return Reflect.metadata("key", value);
};
// 读取metadata
Reflect.getMetadata("key", obj, name)
访问器装饰器
- 能够间接的给属性增加转换器
import 'reflect-metadata';
export default function (logicTypeName: string) {
console.log(arguments);
return function (target: any, key: string, descriptor: PropertyDescriptor) {
descriptor.get = function () {
return this[logicTypeName] + "___";
};
};
}
export class FlowParamDesc {
@ReaderField("逻辑类型")
logicType?: string;
@ReaderField("逻辑值")
logicValue?: string;
@LogicTypeGetter("logicType")
get getLogicType() {
return "";
}
}
const f = new FlowParamDesc();
f.logicType = "123";
console.log(f); // {"logicType":"123"}
console.log(f.getLogicType); // 123___
console.log(JSON.stringify(f)); // {"logicType":"123"}
方法装饰器