Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image in VueJS component is not loading

I got a parent component which sends a data object to the children component like this:

<child object-data=" url: 'url here', title: 'Title'"></child>

Then on my children component, I get this object data by doing:

<script>
    export default {
        props: [
            'objectData'
        ]
    }
</script>

Now, for some reason, I can use title without any problem like this {{ objectData.title }} and shows up.

But when it comes to the URL inside an img tag it's not rendering the image.

I attempted doing the following:

<img :src="objectData.url"/> <--- not rendering

<img v-bind:src="objectData.url"/> <--- not rendering

<img v-bind:src="require(objectData.url)"/> <-- throws a warning error because it's not a path but an object I guess.

<img v-bind:src="{objectData.url}"/> <--- throws an error

<img v-bind:src="{{objectData.url}}"/> <--- throws an error

When I check on the dom element, it doesn't even contain a src attribute afterwards.

If I write down the URL without an object, it works.

<img v-bind:src="src/assets/images/icon.png"/>

But I want my URL to come from a parent component.

Any ideas on how to solve this? I added the url-loader on my webpack file:

            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader'
            }

I also attempted returning objectData.url from a computed/methods fuction:

computed: {
     getImageUrl: function() {
           return objectData.url;
     }
}

And then use it like :src=“getImageUrl” or :src=“{{getImageUrl}}” and I wasn’t lucky either.

like image 707
msqar Avatar asked Dec 08 '18 12:12

msqar


2 Answers

I faced the same issue and i fixed it by using require function :

in the parent component App.vue :

<carousel-posts :posts="posts" />

export default {
 name: "app",
data() {
  return {
   posts: [
     {
      img: require("./assets/logo.png"),
      title: "Title 1",
      subTitle: "Sub Title 1",
      body:"lorem ipsum ..."
    }
    ...
    ]
    };
   }
  ...
 }

in the child component i loop through the posts and bind the image src as follows :

    <div class="card-body" v-for="(post,idx) in posts" >
      <img class="card-img" :src="post.img" />
         ...
     </div>

        <script>
           export default {
            props: ["posts"],
           ...

So in your case you should have something like :

     <child :object-data="objectData"></child>

        ...
     data(){ 
          return{
            dataObject:{
              url: require('./assets/someimg.png'), 
              title: 'Title'
             }
            }
          }

Update :

my project tree :

   src
   |_assets
   |   |_logo.png
   |_components
   |   |_CarouselPosts.vue
   |_App.vue
like image 108
Boussadjra Brahim Avatar answered Sep 29 '22 17:09

Boussadjra Brahim


Two alternatives:

  1. Static links that vue resolves. But they're src-based and don't work with webpack / file-loader:
<img :src="'../assets/filename.png'"/>
  1. Works with webpack. Note the ".default" at the end:
<img :src="require('../assets/filename.png').default"/>

Documentation relevant effective Nov 2019: https://github.com/vuejs/vue-loader/issues/1612

like image 39
Amin Ariana Avatar answered Sep 29 '22 15:09

Amin Ariana