P粉3109311982023-08-28 00:02:02
Solution for dynamic src binding:
<script setup> import imageUrl from '@/assets/images/logo.svg' // => or relative path </script> <template> <img :src="imageUrl" alt="img" /> </template>
<script setup> const imageUrl = new URL(`./dir/${name}.png`, import.meta.url).href </script> <template> <img :src="imageUrl" alt="img" /> </template>
Due to aggregation limitations, all imports must start relative to the import file and should not start with a variable.
You must replace the alias @/
with /src
<script setup> const imageUrl = new URL('/src/assets/images/logo.svg', import.meta.url) </script> <template> <img :src="imageUrl" alt="img" /> </template>
Here's what works for me on local and production builds:
<script setup> const imageUrl = new URL('./logo.png', import.meta.url).href </script> <template> <img :src="imageUrl" /> </template>
Please note that it does not work with SSR
Vite Documentation: New URL< /p>