[Vuejs]-How to save image link in when using 'require' to display image in VueJS?

0πŸ‘

βœ…

The reason it was not working was because

Webpack's require() needs at least some part of the file path to be completely static and we should "make only part of the path dynamic"

that means you cannot put the entire image path in the DB and in the UI pass it to require and expect it to work

I replaced

<img :src="require(`${item.imgUrl}`)" alt="Image"/>

where item.imgUrl = β€˜../assets/entities/pizza/pizza_1.jpgβ€˜ (which is the entire image path relative to the component)

by

<img :src="require(`../assets${item.imgUrl}`)" alt="Image"/>

where item.imgUrl = β€˜/entities/pizza/pizza_1.jpgβ€˜

This answer mentioned by Michal Levy up in the comments explains all this
https://stackoverflow.com/a/64208406/8147680

Leave a comment