我有一个问题,当从对象中获取价值。我需要得到一个值,你的关键是从其他对象的值。
但在打字稿显示此错误。
这是可能得到的值,而无需修改对象类型?
public data: string | {};
解决了投给任何。
常数数据:任何= this.row.data;
...
mounted() {
this.row.columns.forEach(element => {
if (element.show === this.headerMobile) {
this.titleLabel = element.label;
const data: any = this.row.data;
this.titleLabelValue = data[element.show];
}
});
}
...
THX @JGFMK
使用typeof
- 它将返回string
或object
。如果返回对象,那么你可以使用key语法,否则值将是你可以直接使用的变量。
s = 'jeremy'
o = {'s':'gordon'}
a = [s,o]
console.log(typeof(a[0])) // string
console.log(typeof(a[1])) //object