Reputation: 1160
I have been using Vue for a while now, and I noticed that I can store project images in either the assets
folder or the public
folder.
To access images from assets
folder in Vue, one would do :src="require('@/assets/images/myimage.jpg')"
To access images from public
folder in Vue, one would do :src="./static/images/myimage.jpg"
What's the proper location to store Vue project images?
What's the implication of using either?
How do they affect my project?
Upvotes: 26
Views: 15534
Reputation: 447
Based on the official vue documentation everything that is within the assets folder will be handled by Webpack while everything in the public folder is simply copied but doesn't go through Webpack: https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling
So if you want to take advantage of Webpacks optimization features during build (which you most certainly want) put them in assets. I also put all my img's in the assets folder and only have the pwa related images and icons within the public folder.
Upvotes: 22
Reputation: 600
https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling
Different between both would be relative (eventually as require('image')
) vs absolute paths. Usually absolute path will not be passed via webpack during the build. And if you optimize images of absolute path
, nothing will happen.
Upvotes: 1
Reputation: 387
All Stuff in the public folder gets straight up copied to your /dist usually. ex: favicon.ico ends up on your /dist folder with the name... favicon.ico.
The assets folder is usually where you would put your images/videos/fonts/svgs etc that you will import within vue files. These files end up on your /dist folder as a file with a hash name: (ex: 5j2i35j2o251hia.png).
Any assets from the /assets folder that you do not explicitly import will not end up in your /dist folder, as to not bloat your final file size.
Hope this helps..
Upvotes: 11