typescript - 在 typescript 中读取类装饰器信息
问题描述
我有一个用属性装饰的类。
我想阅读构造函数(或其他方法)中的属性。
我已经尝试在该类上使用 Reflect 和所有可能的方法,但我只得到未定义
这个类看起来像这样
@inject(Element)
@bindable('color')
export class Messagebarhost {
public element: HTMLElement;
constructor(element) {
console.log(Reflect.getMetadata("design:paramtypes", this));
// prints undefined
// somehow I want to read the value 'color'
}
}
如果我在创建装饰器的捆绑 js 文件中设置断点。如果我在调试器中输入正确的值,下面的行将打印正确的值
r.__metadata__.undefined["aurelia:resource"].properties[0]
解决方案
我认为你不应该把装饰器放在类上,而应该放在一个字段上,如下所示:
@inject(Element)
export class Messagebarhost {
public element: HTMLElement;
@bindable color;
constructor(element) {
// this.color is not bound here yet.
}
attached () {
// You need to wait till the attached or the bind function is
// called to get the correct value in this.color.
}
}
推荐阅读
- javascript - 如何在数组之间设置 math.random
- unity3d - 如何修复“在地面检测时自动生成游戏对象”
- c# - 使用 NuGet 和单声道解析命名空间
- mongodb - 在 MongoDB 中查找作为名称/值对而不是数组元素的值的子文档
- javascript - 将交互从 D3 v3 更改为 v4
- javascript - Google Analytics Reporting API 目标XXCompletions
- python - 通过 ebay API Python 增加商品搜索结果的数量
- mysql - 在mysql中选择给定时间范围内的最高和最低温度
- javascript - 满足输赢条件后游戏不会重置
- javascript - 当访问者在页面之间移动时,如何使 toggleClass 更改持续存在?