3

In a VueJS with TypeScript and Vuetify app, I have ten pictures called picture1.png, picture2.png, picture3.png, etc. I'm trying to make <div><img src="../assets/pictures/picture1.png"/></div> for each of them. Here's my code:

<template>
    <div class="home">
        <div class="pictures">
            <div v-for="index in Array.from({length: 10}, (v, k) => k+1)" :key="index">
                <img src='../assets/pictures/picture{{index}}.png'>
            </div>
        </div>
    </div>
</template>

This has the error that interpolation in attributes has been removed. As explained to answer this question, you're supposed to use v-bind instead:

<template>
<div class="home">
    <div class="picture">
        <div v-for="index in Array.from({length: 10}, (v, k) => k+1)" :key="index">
            {{index}}
            <img :src="'../assets/pictures/picture' + index + '.png'">
        </div>
    </div>
</div>

That outputs ten broken images, as an example, with src literally like this: src="../assets/pictures/picture1.png"

And that file doesn't exist, so it shows the broken image icon.

If I just use <img src='../assets/pictures/picture1.png'>, it works, and the image url as shown in the dom inspector is <img src="/img/picture1.ea361a2e.png">.

Is there a way to properly handle building img srcs in Vue dynamically but still allow it to handle it the way it handles normal srcs without dynamic binding?

2
  • I don't understand why the working url that you see in the dom inspector is different in path and filename. Either way, you could try :src=`path/to/image${index}.png`. Commented Jun 8, 2018 at 2:51
  • @johan would it make a difference that I'm using a project generated with vue-cli 3 and npm run serve? Commented Jun 8, 2018 at 3:19

1 Answer 1

7

I've had luck doing the following.

:src="require(`images/image-${index}.png`)"
Sign up to request clarification or add additional context in comments.

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.