Webpack will import static assets like ../assets/doggos/riley.jpg as so-called module requests, which means they are handled by a matching Webpack loader defined through a Webpack config. redirect to login page in Vue.js. While it is sometimes necessary, for example for preview images which need a fixed URL, it brings no benefits for our use case.
And I find that the first 2 paragraphs in here explain why this works? The whole problem with variable inside require statement is that require statement is executed during bundling and variable inside that statement appears only during app execution in browser.
I have a case where in my Vue.js with webpack web app, I need to display dynamic images.
The example below shows how to get a dynamic image from the third-party service when you click on the Get a dynamic image button.
The difference between the two folders assets and public/static, and the reason why the first attempt failed, is Webpack.
This also includes the handling of static assets, like in our initial component example. Please note that it's a good idea to put your pet pictures inside a subdirectory, instead of lobbing it in with all your other image assets. Both, vue-cli and Nuxt have configured the handling for multiple file types out of the box, including images with different extensions like jpg, png or gif. Cheers,Renat Galyamov, Want to share this with your friends?renatello.com/bind-img-src-vue-js.
Try importing image like this , because Webpack should know about its dependancy. If we move our dog images into the public folder (or the static folder in Nuxt), and use the code from above with the new folder reference (/public/doggo/${selectedDog.toLowercase()}.jpg), “it works”. That variable is a computed property which is returning a Vuex store variable, which is being populated asynchronously on beforeMount. Why don't self-closing script elements work? Really? Why does the VIC-II duplicate its registers?
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy. We solved it! Let us take a look at the DOM. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. By using require and a strict expression, you will never have problems with dynamic images again!
The public/static folder as a possible workaround (usually not recommended). And we will also re-use our dogImage from above as fallback image. You can also use our online editor to edit and run the code online. But you are missing out some in-depth information ? We might hit caching issues. The name format is always
Does "a point you choose" include any movable surface? (C64). What if we want to swap out the image for Marvin when he grew up a little?
And of course toggle the value of the boolean through some event handler. rev 2020.11.2.37934, Stack Overflow works best with JavaScript enabled, Where developers & technologists share private knowledge with coworkers, Programming & related technical career opportunities, Recruit tech talent & build your employer brand, Reach developers & technologists worldwide, According to nickmessing: "The expression inside v-bind is executed at runtime, webpack aliases at compile time.". You example even works without this function, see here: In my case, pics is being populated asynchronously using Vuex store, may be that has something to do about it, I tried to simulate it, but did not work: The Overflow #45: What we call CI/CD is actually only CI. Learn the basics of binding data to the HTML image tag using Vue.js. How many people voted early (absentee, by mail) in the 2016 US presidential election? dynamic require didn't work for me too in the latest Vue2.
Like so: ./assets/pets/. ~ Renat Galyamov
The only thing our components needs is a template with a single image tag pointing to the path. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. well the best and easiest way that worked for me was this of which i was fetching data from an API.. the getPic method takes one parameter which is the name of the file and it returns the absolute path of the file maybe from your server with the file name simple... here is an example of a component where i used this: I encountered the same problem.