最近做项目的时候,发现执行npx run dev 和 npx run build 结果,dev环境下土拍你资源在build后,不能访问了,研究发现是因为资源路径的问题
assets 和public 目录功能是不一样的,官方文档上有说明: Nuxt uses two directories to handle assets like stylesheets, fonts or images.
The public/ directory content is served at the server root as-is. The assets/ directory contains by convention every asset that you want the build tool (Vite or webpack) to process.
重点:Nuxt won't serve files in the assets/ directory at a static URL like /assets/my-file.png. If you need a static URL, use the public/ directory.
[官网]https://nuxt.com/docs/getting-started/assets
研究发现:
<img class="avatar" src="~/public/img/user_image.jpg" alt=""> //加了public目录以后,这个写法也可以 (直接把saaets换成public)
<img class="avatar" src="/img/user_image.jpg" alt=""> // 这个才是官方用法,就是直接访问public下的资源
