-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Comments
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' |
@LinusBorg does this seem logical or even easy to reason about ? Let me explain : 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 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 |
I hope that I get a answere here, though its closed. |
Please use the forum, as I mentioned in my last reply. This is a closed isssue. |
<img :src="require(`@/assets/${posts.img}`)" alt=""> 🥇 |
Hi @kaankucukx , |
@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 |
Thanks @kakahikari <div class="left" :style="{backgroundImage: `url(${require(`../assets/img/${image}.png`)})`}"> |
@kaankucukx may I ask what the $ sign means here? This also worked with my Vue app and I don't know what's happening. |
you can get more info on link as kaankucukx mentioned: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings |
@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 |
|
@kaankucukx you saved my morning 🚀 |
I keep getting the |
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 |
OMG I've been researching this for 3 hours. Finally stumbled on this. Works! Thank you! |
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
tested on vue cli 3 |
@aacassandra Thank you! So good luck with that. I believe that is uglier :( |
thank you @kaankucukx, you remind me. I just found out that the way it loads all the images. |
I did something like I'm using: |
@fralonra you are missing quotes on Your |
@TotomInc Thanks for your reply! |
Try this: file.html file.js |
@routbiplab There is a trick can do it: However, if all images are under a same folder like But I still wonder if there is a way to archive this goal without doing these tricks? |
the alt attribute is working fine but src attrib. is not working. Can anybody explain?????? PLZ!!! |
@baagi-rebel |
Hi, I can't display image, this is my code. |
Oh, I found the solutiton. just install url-loader :( OMG, thank you guys so much. |
This works for me!! thanks |
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
The text was updated successfully, but these errors were encountered: