【问题标题】:Get value from object with Typescript使用 Typescript 从对象中获取值
【发布时间】:2019-02-05 10:32:05
【问题描述】:

从对象获取值时遇到问题。我需要得到一个值,你的键是来自其他对象的值。

Captura 2

Captura 2

但在 Typescript 中显示此错误。

Visual Studio Error

不修改对象类型是否可以获取值?

public data: string | {};

【问题讨论】:

  • 请将代码作为代码发布!不要将代码作为截图发布!

标签: typescript vue.js


【解决方案1】:

使用typeof - 它将返回stringobject。如果它返回对象,则可以使用键语法,否则该值将是您可以直接使用的变量。

s = 'jeremy'
o = {'s':'gordon'}
a = [s,o]
console.log(typeof(a[0])) // string
console.log(typeof(a[1])) //object

【讨论】:

    【解决方案2】:

    通过强制转换为任何解决。

    常量数据:any = 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];
          }
        });
      }
      ...

    谢谢@JGFMK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 2021-10-25
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多