angular - Property Decorator 不替换定义?
问题描述
我有类似于以下情况的代码,我试图替换基于装饰器的属性
export function DecorateMe() {
return function(component: any, propertyKey: string) {
Object.defineProperty(component, propertyKey, {
get: () => {
...
},
set: (value: boolean) => {
...
}
});
};
}
class Parent {}
@Component()
class Child extends Parent {
@DecorateMe()
public property: string;
}
但是在运行时我看到property
定义为正常,隐藏get/set
添加到原型的方法。
我正在使用 Angular,所以我不确定这是否会产生影响。
解决方案
看起来你的功能不是属性装饰器。使用以下代码:
export function DecorateMe(component: any, propertyKey: string) {
var _val = component[propertyKey];
if (delete component[propertyKey]) {
Object.defineProperty(component, propertyKey, {
get: () => {
console.log("Getter called ", _val);
return _val;
},
set: (value: any) => {
_val = value;
console.log("Setter called ", _val);
}
});
}
}
推荐阅读
- visual-studio-code - VS Code 有没有办法导入 Makefile 项目?
- r - 使用 Plotly 图表向 Shiny 应用程序添加自定义标题
- jquery - 几秒钟后,如何使用 jquery 在选择时刷新页面...?
- android - 由于内存不足,gradle 无法构建 android 项目
- ios - arm64 iOS Swift Gstreamer 库 (Xcode 10.1) 不支持 rtspsrc
- cloud-foundry - Spring Cloud 数据流服务器 Cloud Foundry 配置服务器
- javascript - 可以在快递中使用中继吗?
- amazon-web-services - 使用 pem 文件通过 ssh 连接
- php - 使用 Bootstrap 4 在桌面屏幕右侧的菜单
- php - 带有 FPM 配置的 PHP Composer