The image url is printing in console but not rendering to src attribute. How to achieve this with async and await in vuejs?
<div v-for="(data, key) in imgURL" :key="key">
<img :src= "getLink(data)" />
</div>
Where imgURL contains file name and it is collections of file names.
methods: {
async getLink(url){
let response = await PostsService.downloadURL({
imgURL : url
})
console.log(response.data)
return response.data
}
}
I am getting URL from the backend with axios.
That's not possible that way, the process of creating/updating the DOM from the values is a synchronous process. And because of that Vue.js would use the value/object returned by the
getLink
directly as is, which in your case would be a Promise object.To solve the problem you would need to create an own component for those images. In the created callback of that component, you would call
getLink
, in thegetLink
method you then would set the datalink
as soon as the data is received, and that would trigger a rerender.In the template of the image component you would have something like this:
You for sure can now extend that image component to include a loading indicator or something like that.