PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] How to fetch data from object in axios vue

 October 14, 2022     axios, vue.js, vuejs2, vuetify.js     No comments   

Issue

This is my code.

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));

And the response is

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

This is the template i did.

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

How I can get inside "ethereum" and then inside "usd" and fetch only the value?


Solution

setup your data like

  ethPrice: {ethereum{usd:""}}

you need to set it so it remains reactive

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

and then access it like

{{ethPrice.ethereum.usd}}

check the Vue 2 guide on reactivity https://v2.vuejs.org/v2/guide/reactivity.html for a more detailed discussion



Answered By - Keith Nicholas
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing