Trying to dynamically retrieve paths to images from an Array in Vue and showcase lazyloaded images with a v-for loop has raised some challenges. The code functions properly with template literals, but encounters an error stating "
cannot find module: '~/assets/path/to/image'
" when template literals are used within the require
function. Is it feasible to pass dynamic paths to require
? Is this issue related to Vue or JavaScript?
Here's an illustration:
<!-- The HTML -->
<template>
<figure v-for="logo in logos" :key="logo.path" class="picture desktop-only">
<picture>
<source
:data-srcset="require(`${logo.path}?webp`)"
type="image/webp"
/>
<source
:data-srcset="require(logo.path)"
type="image/jpg"
/>
<img
:data-src="require(logo.path)"
class="lazyload"
:alt="logo.alt"
/>
</picture>
</figure>
</template>
// The logos Array
logos: [
{ path: "~/assets/images/berlinLogos/beuthBer.png", alt: "Beuth Hochschule für Technik Berlin"},
{ path: "~/assets/images/berlinLogos/fuBer.png", alt: "Freie Universität Berlin"},
{ path: "~/assets/images/berlinLogos/htwBer.png", alt: "Hochschule für Technik und Wirtschaft Berlin"},
{ path: "~/assets/images/berlinLogos/huBer.png", alt: "Humboldt Universität Berlin"},
{ path: "~/assets/images/berlinLogos/tuBer.png", alt: "Technische Universität Berlin"},
{ path: "~/assets/images/berlinLogos/uniPots.png", alt: "Universität Potsdam"}
]
This scenario works:
<figure class="picture desktop-only">
<picture>
<source
:data-srcset="require(`~/assets/images/berlinLogos/beuthBer.png?webp`)"
type="image/webp"
/>
<source
:data-srcset="require('~/assets/images/berlinLogos/beuthBer.png')"
type="image/jpg"
/>
<img
:data-src="require('~/assets/images/berlinLogos/beuthBer.png')"
class="lazyload"
:alt="logo.alt"
/>
</picture>
</figure>
Appreciate the assistance!
UPDATE:
Resolved with the following solution:
<figure v-for="logo in logos" :key="logo.path" class="picture desktop-only">
<picture>
<source
:data-srcset="logo.webp"
type="image/webp"
/>
<source
:data-srcset="logo.path"
type="image/jpg"
/>
<img
:data-src="logo.path"
class="lazyload"
:alt="logo.alt"
/>
</picture>
</figure>
logos: [
{ path: require('~/assets/images/berlinLogos/beuthBer.png'), webp: require('~/assets/images/berlinLogos/beuthBer.png?webp'), alt: "Beuth Hochschule für Technik Berlin"},
{ path: require("~/assets/images/berlinLogos/fuBer.png"), webp: require('~/assets/images/berlinLogos/fuBer.png?webp'), alt: "Freie Universität Berlin"},
{ path: require("~/assets/images/berlinLogos/htwBer.png"), webp: require('~/assets/images/berlinLogos/htwBer.png?webp'), alt: "Hochschule für Technik und Wirtschaft Berlin"},
{ path: require("~/assets/images/berlinLogos/huBer.png"), webp: require('~/assets/images/berlinLogos/huBer.png?webp'), alt: "Humboldt Universität Berlin"},
{ path: require("~/assets/images/berlinLogos/tuBer.png"), webp: require('~/assets/images/berlinLogos/tuBer.png?webp'), alt: "Technische Universität Berlin"},
{ path: require("~/assets/images/berlinLogos/uniPots.png"), webp: require('~/assets/images/berlinLogos/uniPots.png?webp'), alt: "Universität Potsdam"}
]