1

您好我正在使用一个返回如下的api

{"secret-finance":{"usd":0.04883396}}

问题是我正在使用 vue 并检索这样的数据

async getCurrentSefiPrice() {
  await axios
    .get(
      "https://api.coingecko.com/api/v3/simple/price?ids=secret-finance&vs_currencies=usd"
    )
    .then(
      (response) =>
        (this.sefi_token_current_price = response.secret-finance.usd)
      // console.log(response)
    );
  console.log(this.sefi_token_current_price);
}

但是当我secret-finance用来获取美元值时,我得到了一个错误。提前致谢。

4

1 回答 1

1

@haseebsaeed 您需要将其引用为

response["secret-finance"].usd

当键中有连字符时,您需要使用键表示法而不是点表示法。任何包含 JavaScript 变量中不允许的字符的键,您都需要像上面那样引用它们。

再举一个例子,如果secret-finance对象的属性键是us-dollars而不是当前的usd,那么您可以通过执行以下操作来访问它,

response["secret-finance"]["us-dollars"]
于 2021-09-18T04:27:13.770 回答