N
Nuxt3w ago
42savage

How to dynamically import images from public directory?

I have about 500 images, i don't want to write by hand patch to all of this files. Images are located in sub directories for example cats, dogs, elephants. Every single directory contains 5-10 images. Is there any way to import it dynamically?
4 Replies
Adeiko
Adeiko3w ago
Why not just put the images in the public directory and just call them by path? https://nuxt.com/docs/getting-started/assets#public-directory
Nuxt
Assets · Get Started with Nuxt
Nuxt offers two options for your assets.
42savage
42savage3w ago
As i said in first line there is ton of images, would you write 500+ pathes by hand?
Smef
Smef3w ago
Maybe on your server-side you do fs.readdir or fs.readdirsync to get the list of files, and then you can pass the results to your front-end to be displayed https://nodejs.org/api/fs.html#fsreaddirpath-options-callback
42savage
42savage3w ago
Nice, love that tip. Gonna try it out