如何从axios vue中的对象中获取数据

How to fetch data from object in axios vue

这是我的代码。

export default {
 components: {
    draggable,
},
data() {
return {
  ethPrice: null,
 };
},
mounted() {
 axios
  .get(
    "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"      
   )
  .then((response) => (this.ethPrice = response.data));

响应是

{ "ethereum": { "usd": 2037.4 } }

这是我做的模板。

<v-card-text>{{ ethPrice }}</v-card-text>

如何进入“ethereum”然后进入“usd”并只获取值?

一样设置你的数据
  ethPrice: {ethereum{usd:""}}

您需要对其进行设置以使其保持反应性

.then((response) => (this.$set(this.ethPrice,response.data)));

然后像

一样访问它
{{ethPrice.ethereum.usd}}

查看有关反应性的 Vue 2 指南https://vuejs.org/v2/guide/reactivity.html 以获得更详细的讨论