How to load an image src url into Quasar/Vuejs

Asked

Viewed 169 times

0

How do I load an image src template? I tried it this way

<img :src="$axios.get(`/uploader/hotels/${hotel.photo}`)"/>

but the result is a [object Promise] with strange return:

http://localhost:8080/[object Promise]

How do I remove baseUrl from the quasar to show only the api url? What is missing?

I would like to click directly on the tag src of img without having to use the methods. For so I could call the Aces that way on every page that was necessary.

2 answers

0

Whereas you are using the cli.vuejs.org to generate your project and build, I suggest using environment variable files, .env

Mimics the use of Node environment variables.

Then you create your files .env for its environments (development, homologation and production) with the variables:

NODE_ENV=development
VUA_APP_API_BASE_URL=http://localhost:8080/api

VUE_APP_ is the mandatory prefix for variables to be injected into your app

And then in your code just use process.env.VUE_APP_API_BASE_URL

I no longer know if it is possible to use the process.env.VUE_APP_X in expressions in the <template>, but I suggest using computed properties in its components or even creating a component just to abstract it.

<template>
  <img :src="src" />
</template>
<script>
export default {
  name: 'Image',
  props: ['path'],
  computed: {
    src({ path }) {
      return `${process.env.VUE_APP_API_BASE_URL}/${path}`;
    }
  }
}
</script>

And use the component:

<Image :path="`/uploader/hotels/${hotel.photo}`"/>

And in the place where you set the base url of Axios, replace by process.env.VUE_APP_API_BASE_URL.

  • I tested the first option and it does not work in template as it was said. I tested the second and the page gets stuck. The main reason I want to use it this way is because in Axios, the url of the server was already defined and just concatenate with the corresponding location. If the system changes server, I would change in one place. I think creating a component just for that would be too bureaucratic. It will be if I can set a global string type variable at a location in the quasar framework just to access the server address anywhere in the system?

  • Relax, I think now I understand... Did you want to use only the url already set in Next as default.baseUrl to concatenate the url you put in src? Or you want to call a service that a url and finally use the returned url of the service as src of the image?

  • Only the url solves the problem. I was searching if there was a place to put these variables so I could also use in Xios.

  • Okay, so you just need to use the url... quiet, I’ll edit my answer

  • The proposed solution is interesting! Because it used as a base . env that can be changed easily for each environment! However, I found it strange to have to redo the img tag as Component. It would not be more interesting for the process.env.VUE_APP_API_BASE_URL direct on Xios?

  • Yes, I mentioned the Image component as an example. It’s just that I personally don’t find it interesting to keep using the $axios.defaults.baseURL... I would already set out for the strategy of a specific component to abstract this work of always putting the process.env.VUE_APP_API_BASE_URL or create a type function resolveImgSrc and always use it to mount the path.

Show 1 more comment

-1

I was able to find a solution, Vuex itself has a global variable to access the url inside the plugin Axios:

import axios from 'axios';
axios.defaults.baseURL = 'http://serverbackend:8765';

export default ({ Vue }) => {
  Vue.prototype.$axios = axios;
};

Soon the call in the url was:

<img :src="`${$axios.defaults.baseURL}/uploader/hotels/${hotel.photo}`"/>

Problem solved :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.