如何使用Svelte将所有文件获取目录中的所有文件?

发布于 2025-02-04 04:12:09 字数 224 浏览 3 评论 0原文

我想在文件夹中显示所有图像,例如:

<script>
   let list = /*array of all files in a folder*/;
</script>

{#each list as l}
    <img src={(path_of_l)} alt=""/>
{/each}

如何获得该元素的“列表”及其路径?

I want to show all images in a folder, like:

<script>
   let list = /*array of all files in a folder*/;
</script>

{#each list as l}
    <img src={(path_of_l)} alt=""/>
{/each}

How can I get that 'list' and paths of its elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2025-02-11 04:12:09

当图像在您的项目文件夹中并且使用Vite(或Sveltekit)时,您可以使用 Glob Import 功能:

const imageModules = import.meta.glob("../../static/*.jpg");

  for (const modulePath in imageModules) {
    imageModules[modulePath]().then(({ default: imageUrl }) => {
      console.log(modulePath, imageUrl);
    });
  }

When the images are inside your project folder and you're using Vite (or SvelteKit) you can use the Glob Import feature:

const imageModules = import.meta.glob("../../static/*.jpg");

  for (const modulePath in imageModules) {
    imageModules[modulePath]().then(({ default: imageUrl }) => {
      console.log(modulePath, imageUrl);
    });
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文