0

I am trying to show images through v-for, but they do not appear.

<div v-for="(n, i) in images" :key="i">
    <v-img :src="{n}"></v-img>
</div>

...

images: [
     '../assets/thirdSection/Olivia.png',
     '../assets/thirdSection/Olivia.png',
     '../assets/thirdSection/luiz.png',
]
5
  • You really should not use i for the :key, it will be worse than using nothing. Commented Apr 1, 2021 at 12:05
  • @kissu Where are you reading that from? That’s totally untrue. As long as i is a string, it is actually recommended to use it as the key. Commented Apr 1, 2021 at 12:08
  • @Terry based on how it works essentially, but this is a good source to corroborate my tellings: stackoverflow.com/a/44531545/8816585 Commented Apr 1, 2021 at 12:12
  • 1
    please share your folder structure (where the componet is related to the images) Commented Apr 1, 2021 at 19:00
  • please show the full component code Commented Apr 1, 2021 at 20:41

2 Answers 2

3

You do not need to use {} juse remove those brackets from your :src binding

<div v-for="(n, i) in images" :key="i">
    <v-img :src="n"></v-img>
</div>

...

images: [
     '../assets/thirdSection/Olivia.png',
     '../assets/thirdSection/Olivia.png',
     '../assets/thirdSection/luiz.png',
]
Sign up to request clarification or add additional context in comments.

7 Comments

man, still not going. I tested it to see if the image path is correct and yes, it is. But it still doesn't appear
@VitorLucenti is your path correct ? Try to display one (just grab the first index) before trying to loop on the array. <v-img :src="images[0]"></v-img>
you probably need to use ~/assets
Nothing yet guys :( The image path is right
Is vuetify properly installed ? Check it with a regular <img> tag.
|
0

Try using require to load your local images:

<div v-for="(n, i) in images" :key="i">
    <v-img :src="require(n)"></v-img>
</div>

...

images: [
     '@/assets/thirdSection/Olivia.png',
     '@/assets/thirdSection/Olivia.png',
     '@/assets/thirdSection/luiz.png',
]

You just have to install file-loader to your modules and configure your webpack.

Doing it like that, your local image path is resolved into a url.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.