Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Reactive img src in Vue via API

I'm wondering, how should i solve this problem. I have api which is returning base64 image, and after entering the site, i want to load this img, any one have clues how or where should i put my function?

this is my api call which is in methods:

methods:{
    async getGraph(){
      const body = new FormData();
      body.append('hostname','hostname');
        axios({
          method:'post',
          url:'http://127.0.0.1:8000/api/graph',
          data: body,
          headers:{"Content-Type":'multipart/form-data'}
        }).then(response=>{
                var graphBase64 = Object.values(response.data)[0]
                console.log(graphBase64)
                return graphBase64
        }).catch(function(response){
          console.log(response)
        })
    }
}

and i want to have it in this:

<img v-bind:src="getGraph()">

i was thinking maybe my api call should be in beforeMounted but after the site wouldn't load Huge thanks for any clues/articles/ideas !

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you are close. you are binding to make it reactive but the wrong way.

async functions returns Promise: MDN docs.

You can instead create another variable instead and assign the value to that:

<template>
  <img :src="base64" />
</template>

<script>
data() {
  return {
    base64: "",
  }
},

mounted() {
  this.getBase64()
},

methods: {
  async getBase64() {
    // do async/promise stuff
    this.base64 = someValue
  }
}
</script>

Docs for the mounted() hook: https://v2.vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda