angular - 如何在 Typescript 中使用代理为新属性添加 getter
问题描述
我有一个对象,我想向其中添加一个新属性:
this.exampleConfig = new Proxy(config, {
set: (obj, prop, value) => {
...
}
}
我在 Angular 应用程序中使用它,目前有一个exampleConfig.myProperty
. 我想添加一个exampleConfig.myExtendedProperty
我试过了
this.fileConfig = new Proxy(config, {
set: (obj, prop, value) => {
...
},
get: function (obj, prop, receiver) {
if (prop === "myExtendedProperty") {
return obj.myProperty.toUpperCase()+'_EXTENSION';
}
return Reflect.get(...arguments);
},
});
但是,编译器会抛出:
Error ... : Expected 2-3 arguments, but got 0 or more.
return Reflect.get(...arguments);
node_modules/typescript/lib/lib.es2015.reflect.d.ts:26:18
26 function get(target: object, propertyKey: PropertyKey, receiver?: any): any;
~~~~~~~~~~~~~~
An argument for 'target' was not provided.
我应该怎么办?
解决方案
正如下车我的草坪所建议的,这可以解决
return Reflect.get(obj, prop, receiver)
所以在这种情况下
get: function (obj, prop, receiver) {
if (prop === "myExtendedProperty") {
return obj.myProperty.toUpperCase()+'_EXTENSION';
}
return Reflect.get(obj, prop, receiver)
},
推荐阅读
- python - 在归一化时间序列分析上绘制滚动平均值
- web-scraping - 使用刮板箱检索兄弟元素
- python - 从另一个网络连接到 XAMPP MySQL
- python - PyQt6 Python:使用按钮运行脚本
- java - 代号名称 iOS 和 Android 构建错误
- node.js - Cordova - ReferenceError globalThis未定义
- jvm - 为 Java 编写的引导加载程序
- apache-nifi - 如何使用状态更新从 dev 到 prod 的流程
- java - Spring Boot invalidDataAccessApiUsageException - OUT/INOUT 参数不可用
- php - 我怎样才能把剩下的数据放在一些 PHP 下