1

I can't get my image to show up for some reason. The path of the image should be sent to the component and rendered there. But it just doesn't render. I believe I am using the Vue-CLI if that helps.

This is my folder structure:

src | App.vue | bulbasaur.png | /components | Pokemon.vue 

App.vue

<template> <div id="app"> <Pokemon location="./bulbasaur.png"/> </div> </template> <script> import Pokemon from './components/Pokemon.vue' export default { name: "app", components: { Pokemon } }; </script> <style> </style> 

Pokemon.vue

<template> <div> <img :src="`${location}`" /> </div> </template> <script> export default { name: 'Pokemon', props: { location: String } }; </script> <style scoped> </style> 
7
  • Are you getting any error in console or network tab due to image path is not correct, etc? Commented Jun 22, 2020 at 3:59
  • No, there is no error. The element is created as: <img data-v-3848655a="" src="./bulbasaur.png"> but only a broken image link is displayed. Commented Jun 22, 2020 at 4:01
  • Can you try once to put the images into public folder instead like this: html-and-static-assets Commented Jun 22, 2020 at 4:04
  • 1
    Please read the docs fully. In templates, you will need to first pass the base URL to your component. Then you can use it like: <img :src="${publicPath}my-image.png"> Commented Jun 22, 2020 at 4:16
  • 1
    Ah, thank you! I finally got it working! I had to add data () {return {publicPath: process.env.BASE_URL} in my export default and then it worked! Thanks! Commented Jun 22, 2020 at 4:30

1 Answer 1

3

First, create an image prop in your child component:

props: { image: { type: String, default: "" } } ... 

Add the img tag in the child component:

<img :src="image" /> 

Import the image in the parent component:

import MyImage from '../path/to/my/image'; 

For Vue3, make sure to provide the loaded image to the template:

setup() { return { MyImage }; } 

For Vue2, you can add it in your data section:

data() { return { MyImage: MyImage }; } 

Then just pass the image to your child prop:

<child :image="MyImage" /> 
Sign up to request clarification or add additional context in comments.

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.