【问题标题】:How to fetch data from object in axios vue如何从 axios vue 中的对象中获取数据
【发布时间】:2021-09-22 00:23:41
【问题描述】:

这是我的代码。

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”并只获取值?

【问题讨论】:

    标签: vue.js vuejs2 axios vuetify.js


    【解决方案1】:

    像这样设置你的数据

      ethPrice: {ethereum{usd:""}}
    

    您需要设置它以使其保持反应状态

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

    然后像访问它

    {{ethPrice.ethereum.usd}}
    

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

    【讨论】:

    • 对不起,但是用你的代码我得到了这个错误。 prnt.sc/1amh2lx
    • 嗨,现在它可以工作了,但我不得不跳过反应式.then((response) =&gt; (this.ethPrice = response.data)); 和像这样声明的对象。 ethPrice: { "ethereum": { "usd": "" } }谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 2021-09-22
    • 2021-07-22
    • 2020-10-21
    • 1970-01-01
    • 2021-08-14
    • 2023-03-17
    相关资源
    最近更新 更多