在Angular 8中,我从JSON创建了一个对象,有一个动态属性,在运行时之前我不知道它的名称。在另一个对象内部,我需要调用该动态属性的值,但是由于该值在运行时发生,因此不存在,因此将无法编译。
有没有办法做到这一点?
let i = 0;
this.columns = [
{ name: 'total' },
];
do {
if (certainCondition) {
this.column.push({ name: res.data[i].text })
}
this.rows.push({
total: someNumber,
res.data[i].text: someValue
});
i++;
} while (res.data[i] != null)
您需要定义其类型,或者只是使用它any
来加速刺痛。在使用res.data[i].text
作为重点用方括号[res.data[i].text]: value
。
(res: any) => {
let i = 0;
this.columns = [
{ name: 'total' },
];
do {
if (certainCondition) {
this.column.push({ name: res.data[i].text });
}
this.rows.push({
total: someNumber,
[res.data[i].text]: someValue
});
i++;
} while (res.data[i] != null);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句