0

I have an array of plants. Each plant is an object that includes its name, characteristics, and its image path. I want to show them in a v-for list.

What I've tried:

<img v-bind:src="plant.img">

and this:

<img v-bind:src="'./../../assets/plants/' + plant.name + '.jpg'">

Neither showed the image.

But when I put the image path, like this:

<img src="./../../assets/plants/rose.jpg">

It shows.

It's not v-for error too. What's wrong with my code?

data() {
return {
  plants: [
    { name: 'Rose', sun: 4, water: 6, care: 5, img: "./../../assets/plants/rose.jpg" },
    { name: 'Mint', sun: 8, water: 3, care: 4, img: "./../../assets/plants/mint.jpg" },
    { name: 'Thyme', sun: 7, water: 4, care: 3, img: "./../../assets/plants/thyme.jpg" },
    { name: 'Oregano', sun: 4, water: 6, care: 5, img: "./../../assets/plants/oregano.jpg" },
    { name: 'Lavanda', sun: 8, water: 3, care: 4, img: "./../../assets/plants/lavanda.jpg" },
    { name: 'Patchouli', sun: 7, water: 4, care: 3, img: "./../../assets/plants/patchouli.jpg" },
  ]
}};
3
  • Can you show your plant object Commented Jun 7, 2017 at 8:18
  • I tried to lowercase the name too. Commented Jun 7, 2017 at 8:30
  • I think This has to do with how dynanamic images are loaded in webpack maybe. Have a look at the answer here(stackoverflow.com/questions/40491506/…), your problem will be solved Commented Jun 7, 2017 at 9:08

2 Answers 2

3

Hi Try this one maybe its a webpack issue

<img :src="getSrc(plant.name)" v-bind:alt="pic">

And add to methods.

methods: {
    getSrc(name) {
        var images = require.context('../../assets/plants/', false, /\.jpg$/);
        return images('./' + name + ".jpg")
    }
}

https://jsfiddle.net/khenxi/vcf57d0f/

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

1 Comment

I just changed the path to static folder and this fixed the problem. Thanks.
0

I imported my image:

import logo from '@/assets/images/logos/my-logo.png';

My object in operators array:

{
      id: 1,
      name: 'logo',
      logo: logo,
      tpe: 22,
    }

and then use it with v-bind :

            <img :src="operator.logo" height="150" alt="operator logo" />

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.