Skip to content

Vue-cli compile image src via dynamic property #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
khaled0fares opened this issue Jan 18, 2017 · 31 comments
Closed

Vue-cli compile image src via dynamic property #450

khaled0fares opened this issue Jan 18, 2017 · 31 comments

Comments

@khaled0fares
Copy link

When i reference the image source via the assets directory it works as expected and compile the image path to static one like this /static/img/img.1e7c8df.jpg

but when i use a dynamic property and pass the value of the property via the parent component,
the image path doesn't compile to /static/img/img.1e7c8df.jpg path

screen shot 2017-01-18 at 7 26 35 pm

screen shot 2017-01-18 at 7 30 49 pm

@LinusBorg
Copy link
Contributor

you have to actually import the image in JS. then webpack knows about it as a depenency and can manage the path.

import Image from './assets/image.jpg'
// `Image` will not be a string, pointing to  '/static/img/img.1e7c8df.jpg'

@darkylmnx
Copy link

darkylmnx commented Jul 28, 2017

@LinusBorg does this seem logical or even easy to reason about ? Let me explain :
Assuming i have a team page on my web app, i have 50 teammates, it would be anoying to write 50 times a div + h2 + img + some description.

So i would use a loop, my image would go from 1 to 50 so i would dynamically use :src with the index of the loop.

BUT those images would be in my assets, so are you expecting someone to write 50 times and import for his images ?

Knowing that we can't loop over an import
image

here's a fiddle of what i mean : https://jsfiddle.net/76sythpu/

i think we need to fin another solution, importing the asset isn't a good idea if you have many dynamic assets.

Copying a folder of assets to the dist folder maybe the only solution for now... I'd like to re open this issue to discuss about that

@LinusBorg
Copy link
Contributor

Well, that's how webpack works for dynamic assets. You still have options, though.

  1. use the /static folder as explained here.
  2. Use a dynamic require to make webpack require all images in one call, as explained here

I won't go into much detail (for that we have forum.vuejs.org).

@maximilianfixl
Copy link

I hope that I get a answere here, though its closed.
I can‘t understand that I can load an image from the laravel /public/ images directory, but not from /storage/app/public!
If image file name and subfolder is dynamic. Could anybody explain it to me? Please!?

@LinusBorg
Copy link
Contributor

Please use the forum, as I mentioned in my last reply.

This is a closed isssue.

@maximilianfixl
Copy link

@kaankucukx
Copy link

kaankucukx commented May 12, 2018

<img :src="require(`@/assets/${posts.img}`)" alt="">

🥇

@david-saint
Copy link

Hi @kaankucukx ,
What if I want to add it to the style tag like
<div class="left" :style="backgroundImage: require(assets/img/${image}.png);">
How should I go about this?

@kaankucukx
Copy link

@david-saint Hey,

Go with this.. ;)

<div class="left" :style="`backgroundImage: require(assets/img/${image}.png)`">

Check this by Addy. https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

@david-saint
Copy link

Thanks @kakahikari
this worked

<div class="left" :style="{backgroundImage: `url(${require(`../assets/img/${image}.png`)})`}">

@nicobaguio
Copy link

@kaankucukx may I ask what the $ sign means here? This also worked with my Vue app and I don't know what's happening.

@nfer
Copy link

nfer commented Sep 11, 2018

@nicobaguio

Template Strings can contain placeholders for string substitution using the ${ }

you can get more info on link as kaankucukx mentioned: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

@kaankucukx
Copy link

@nicobaguio Hey,

its ES6 feature called Template Literals. you can check it from the link @nfer mentions. or below one!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

@ghost
Copy link

ghost commented Oct 7, 2018

<img :src="require(@/assets/login.png)" alt="">
<img src="@/assets/login.png" />
same but shorter

@rimiti
Copy link

rimiti commented Oct 13, 2018

@kaankucukx you saved my morning 🚀

@revolter
Copy link

I keep getting the Cannot find module error, even though the path is correct.

@bigsee
Copy link

bigsee commented Nov 25, 2018

Note that you can also use the following syntax to avoid needing to remember the relative paths...

<div
    class="picture"
    :style="{ backgroundImage: `url(${require(`@/assets/images/${image}`)})` }">

...where @ signifies your src folder and image is the full filename.

@oelbaga
Copy link

oelbaga commented Jan 21, 2019

OMG I've been researching this for 3 hours. Finally stumbled on this. Works! Thank you!

@aacassandra
Copy link

thank you @kaankucukx, your suggestion is to work with charm, but your code is not beautiful with vue framework

thank you @LinusBorg, your advice is to work very beautiful

import Image from "../../assets/img/services/s1.png";
export default {
   data () {
     return {
       myPic: Image;
     }
   }
}

<img v-bind:src="myPic" />

tested on vue cli 3

@kaankucukx
Copy link

@aacassandra Thank you!
And you mean importing all images is fine? :)

So good luck with that. I believe that is uglier :(

@aacassandra
Copy link

thank you @kaankucukx, you remind me. I just found out that the way it loads all the images.

@fralonra
Copy link

fralonra commented Jun 24, 2019

I did something like <img :src="require(@/assets/logo.png)" >
But it gave me an error that Critical dependency: the request of a dependency is an expression
Anyone can help?

I'm using:
webpack v4.28.4
vue v2.6.10
@vue/cli-service v3.6.0

@TotomInc
Copy link

@fralonra you are missing quotes on @/assets/logo.png.

Your :src attribute should looks like this :src="require('@/assets/logo.png')".

@fralonra
Copy link

fralonra commented Jun 25, 2019

@TotomInc Thanks for your reply!
That's just a typo.
I tried :src="require('@/assets/logo.png')" and the warning still exists.

@routbiplab
Copy link

routbiplab commented Jun 26, 2019

Try this:

file.html
<img v-bind:src="image">

file.js
image:require('@/assets/tutorial/onWhite.jpg'),

@fralonra
Copy link

fralonra commented Jun 27, 2019

@routbiplab
It was my fault not pointing out what I want to do.
I want to load image dynamically, by setting image path in a variable like logo.

There is a trick can do it: require('@/assets' + logo), but logo cannot start with '@/assets' in this way.

However, if all images are under a same folder like @/assets, I can set logo begin with '@/assets', and then remove this prefix in the require expression, like: require('@/assets' + this.logo.split('@/assets')[1]).

But I still wonder if there is a way to archive this goal without doing these tricks?

@baagi-rebel
Copy link

the alt attribute is working fine but src attrib. is not working. Can anybody explain?????? PLZ!!!
<food-items itemPrice= 239 itemName="Desert" image="./assets/images/desert.jpg" altText="image here from app"></food-items>// App.vue
<img :src="image" :alt="altText"> //component food-item

@fralonra
Copy link

fralonra commented Jun 28, 2019

@baagi-rebel
See here. #126.
In short, you should use require or import to tell webpack that this path should be treated as a module.

@hoainamcr
Copy link

hoainamcr commented Dec 5, 2019

Hi, I can't display image, this is my code.
<img :src="require('@/assets/images/logo.png')" />
but it just diplay after rendered
<img data-v-359d76e0="" src="[object Module]" class="">
Do you know why it return [object Module]? please help me to resolve it :(

@hoainamcr
Copy link

Hi, I can't display image, this is my code.
<img :src="require('@/assets/images/logo.png')" />
but it just diplay after rendered
<img data-v-359d76e0="" src="[object Module]" class="">
Do you know why it return [object Module]? please help me to resolve it :(

Oh, I found the solutiton. just install url-loader :( OMG, thank you guys so much.

@juniorknx
Copy link

This works for me!! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests