-
Is there any possibility to load dynamic images? I have the following use case: My workaround is a sfc called <template>
<Picture :src="src" />
</template>
<script setup lang="ts">
import { computed } from 'vue'
import event_img_1 from '@/assets/events/event_img_1.jpg?preset=full'
import event_img_2 from '@/assets/events/event_img_2.jpg?preset=full'
import event_img_3 from '@/assets/events/event_img_3.jpg?preset=full'
let assets = new Map<string, any>([
['event_img_1.jpg', event_img_1],
['event_img_2.jpg', event_img_2],
['event_img_3.jpg', event_img_3],
])
const props = defineProps({
name: {
type: String,
required: true,
},
})
const src = computed(() => assets.get(props.name))
</script> It works. But i need to add a line of code every time i add a new event image into the assets directory (what happens sometimes). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Starting in Vite 4, you can use glob imports.
This plugin does provide a programmatic API that can be used directly in Vite plugins: const image = await images.api.resolveImage('@/assets/events/${imageName}`, { preset: 'full' }) For certain use cases this is a reasonable approach, such as when using Moving this to discussions, feel free to ask additional questions. |
Beta Was this translation helpful? Give feedback.
-
Hi @ElMassimo, is there any update on that one? I'm trying to create a component where src it's just a String prop, and you just pass presets as a String Array.
For sure I'm doing something wrong as it cause several errors: Do you think it's possible to just fetch one image as I'm trying to avoid fetching the whole map of images? (eg: within '/public/images/**/*.jpg'). |
Beta Was this translation helpful? Give feedback.
Starting in Vite 4, you can use glob imports.
Unfortunately, Vite currently does not support passing query parameters in glob imports, which would be an automatic way to generate a similar structure to the map in your example. It might be added in the future, perhaps you can suggest it as a feature request in Vite.This plugin does provide a programmatic API that can be used directly in Vite plugins:
For certain use cases this is a reasonable approach, such as when using
extendFrontmatter
in îles.Moving this to discussions, feel free to ask additional questions.