0

I looked at Vue cannot find module image location and couldn't figure out a solution.

in my code when I hard code it

:src="require('@/assets/featured/pizzaOne.jpeg')"

I get the image. But when I try

:src="require(`${item.img}`)"

I get Error: Cannot find module '@/assets/featured/pizzaOne.jpeg' Not sure why and how to fix this

1 Answer 1

1

If you can change item.img to just name pizzaOne.jpeg, then in method:

methods: {
  getImgUrl: function (img) { 
     return require('@/assets/featured/' + img);
  }
}

end in template:

<img :src="getImgUrl(item.img)" />
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution! It works but you need to make sure the method name getImgUrl matches the one used in the <img> tag. So it's either getImgUrl or getImage, or anything you fancy really as long as the names matching.

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.