0

I'm trying to render four images using v-for with a range, 1 to 4. v-for="n in 4" :key"n" But, using 'n' as part of src does not work. Why?

 <div v-for="n in 4" :key="n">
    <img
       :src="`../assets/images/image-product-${n}-thumbnail.jpg`"
       class="rounded-xl h-16 w-16 hover:cursor-pointer"
    />
 </div>

Tried using 'key' instead of 'n', converting 'n' to string... none worked

If I hardcorde like this, it works

<img
      src="../assets/images/image-product-1-thumbnail.jpg"
      class="rounded-xl h-16 w-16 hover:cursor-pointer"
 />
6
  • The error I get is: GET localhost:3000/assets/images/image-product-4-thumbnail.jpg [HTTP/1.1 404 Page not found: /assets/images/image-product-4-thumbnail.jpg 731ms] Commented Jan 8, 2024 at 13:11
  • 1
    Well, the problem is not related to vue or v-for. Vue is doing what you told him to, as the URL is the error is right. Actually, the error hints you toward what the problem is : the image was not found on your local server. Commented Jan 8, 2024 at 13:20
  • Are you sure number 4 is available? Commented Jan 8, 2024 at 13:57
  • Yes, the error appears for 1 to 4 Commented Jan 8, 2024 at 14:51
  • Vue.js questions are highly version specific and should always be tagged with [vuejs2] or [vuejs3]. In the case you're using Vue 3 + Vite please see this question and answer. The problem is not that the URL is incorrect, but that Vite needs specific help in analyzing dynamic URLs Commented Jan 8, 2024 at 14:56

2 Answers 2

0

I had the same issue, but the problem is not the path or the @ in the path, I've tried this:

    <img class="product-listed" :src="`${product.src}`">

    <button @click="navigateTo(product.id)" class="add-to-cart">Añadir al carrito</button>
   <h4> {{product.name }}</h4>
   <p>{{ product.precio }}</p>
</div>

And in the browser DOM this is what I see:

If I place an image manually with the same path, it shows up, but when is inside a dinamically path (v-for) coming from the API, the image does not show up. By the way Im using Vue JS version Option API.

Also I've tried: :src="require(${product.src})" where the product.src is declared in the JSon (API) like following: { "name":"product1", "src":"../assets/img/product2.png", "precio":"15.50", "categoryID":1, "id": "1" },

And if I console log the product, it has the right path. and properties. It works on my local enviroment, maybe it could help you

Sign up to request clarification or add additional context in comments.

1 Comment

Please do not use answers to say you have the same problem/question. Only use answers to post actual solutions/answers to the question being asked.
-2

If you have a global aliases (@) available i recommend you to use it.

Because using relative path can works in local for example but when the project is compile (build) the related path can be broken.

When using dynamic src path you can require it before loading.

With the aliases you can do:

:src="`require(@/assets/images/image-product-${n}-thumbnail.jpg`)"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.