如何从资产文件夹中获取图像?

发布于 2025-01-30 09:28:36 字数 906 浏览 0 评论 0原文

我是初学者,目前正在学习vue.js。我尝试创建一个轮播滑块,但我无法从资产文件夹中获取图像。我找不到任何解决方案,可以帮助我解决问题。谁能帮我,为什么我在网站上看不到?

<template>
 <div class="container">
  <div class="title">Projects</div>
   <Carousel class="carousel">
    <Slide v-for="(slide, index) in carouselSlide" :key="index">
     <div class="slide-info">
      <img :src="require(`../assets/${slide}.jpg`)" />
     </div>
    </Slide>
   </Carousel>
  </div>
</template>

<script>
import Carousel from "../utility-components/Carousel.vue";
import Slide from "../utility-components/Slide.vue";

export default {
setup() {
  const carouselSlide = ["bg-1", "bg-2", "bg-3"];

  return carouselSlide;
},

components: { Carousel, Slide },
};
</script>

在此处输入图像描述

I'm beginner and I currently learn vue.js. I try to create a carousel slider but I can't get my images from the assets folder. I don't find any solution that it helps me to solve the problem. Can anyone help me, why I don't see on the site?

<template>
 <div class="container">
  <div class="title">Projects</div>
   <Carousel class="carousel">
    <Slide v-for="(slide, index) in carouselSlide" :key="index">
     <div class="slide-info">
      <img :src="require(`../assets/${slide}.jpg`)" />
     </div>
    </Slide>
   </Carousel>
  </div>
</template>

<script>
import Carousel from "../utility-components/Carousel.vue";
import Slide from "../utility-components/Slide.vue";

export default {
setup() {
  const carouselSlide = ["bg-1", "bg-2", "bg-3"];

  return carouselSlide;
},

components: { Carousel, Slide },
};
</script>

enter image description here

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

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

发布评论

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

评论(1

葬心 2025-02-06 09:28:36

我找到了解决方案。此代码(require)在vue.js 3:运行:npm run build中不存在

<img :src="require(`../assets/${slide}.jpg`)" />

,您将获得DIST文件夹,可以保存图像,然后从那里拨打。它对我有用。

因此,解决方案是:

<template>
 <img :src="picture.pic" />
</template>

<script>
import { ref } from "vue";

const loadImage = async () => {
 return new Promise((resolve) => {
  resolve({
   pic: "dist/assets/bg-1.jpg",
  });
 });
};
};

export default {
async setup() {
 const picture = ref(await loadImage());

  return {
   picture,
  };
 },
};
</script>

I found the solution. This code (require) doesn't exist in vue.js 3:

<img :src="require(`../assets/${slide}.jpg`)" />

Run: npm run build and you'll get dist folder where you can save your images then call from there. It works for me.

So the solution is:

<template>
 <img :src="picture.pic" />
</template>

<script>
import { ref } from "vue";

const loadImage = async () => {
 return new Promise((resolve) => {
  resolve({
   pic: "dist/assets/bg-1.jpg",
  });
 });
};
};

export default {
async setup() {
 const picture = ref(await loadImage());

  return {
   picture,
  };
 },
};
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文