Import a list of images from public folder
the folder
public/images
has 9 images that I need to use in my script setup
function. I don't wish to import all 9 images separately, can I import the entire folder?10 Replies
Hi @NightFuries. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To import multiple images from the
public/images
folder in Nuxt 3, you don't actually need to import them in the traditional sense. The public
directory in Nuxt is used to serve static assets directly, and its contents are accessible at the root URL of your application.
Here's how you can handle this situation:
1. Place your images in the public/images
folder.
2. In your component, you can create an array of image paths like this:
This assumes your images are named image1.jpg
, image2.jpg
, etc. Adjust the naming pattern if necessary.
3. You can then use these paths in your template:
This approach doesn't require you to import the images explicitly. Instead, it leverages Nuxt's handling of the public
directory.
Remember that files in the public
directory are served as-is and are not processed by the build tools. This is suitable for static assets that don't need processing.
For more information on how Nuxt handles static assets, you can refer to the Nuxt documentation on the public directory.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@711113525023735809> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai the array don't work in dev mode since the image paths are not translated.
Let me check the knowledge base for you again. One moment! :books:
----------------------
I apologize for the confusion in my previous answer. You're right that the approach I suggested might not work as expected in development mode due to how Nuxt handles asset paths. Let's address this issue with a more appropriate solution for Nuxt 3.
In Nuxt 3, when dealing with assets in the
public
folder, it's recommended to use the useAsset
composable or the ~
alias. Here's a corrected approach that should work in both development and production modes:
This approach uses the useAssets
composable, which is part of Nuxt 3's auto-imports feature. It will correctly resolve the paths for your images in both development and production environments.
If you prefer not to use the composable, you can also use the ~
alias:
In this case, we use the ~
alias in the template to ensure proper path resolution.
These methods should work correctly in both development and production modes. For more information on handling assets in Nuxt 3, you can refer to the Nuxt documentation on assets.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@711113525023735809> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
why import? can't you just do
<img src="/path-to-file" />
I am using matter.js. You can find that at https://ichack.org in the right box. So we have 7 svg images in the public directory and we are executing the matter.js code in the client, i.e. inside
onMounted
callback. The (very horrible) code to add images looks like this:
The problem here is, we can't use do:
code by @liabru
Matter.js
Matter.js is 2D rigid body physics engine for the web, using JavaScript and HTML5
So we are feeding the images into a library, not to the image tag. The import option is
Am I making sense here? Is there a better way?
ohh, I see. Maybe you could make a fetch call to download the images and use them, or try the glob way with https://www.lichter.io/articles/nuxt3-vue3-dynamic-images/
www.lichter.io
Vue 3: How to load dynamic images
With the release of Vue 3, a lot of things became easier. Unfortunately, loading dynamic assets such as images is not one of them. In the following article, I want to demystify the process of dynamic asset loading in Vue 3 or Nuxt 3 and explain why static assets can be loaded easily...
Yess this works brilliantly! Nuxt is great