user19154568
user19154568

Reputation:

How to get images from assets folder?

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

Upvotes: 1

Views: 4599

Answers (1)

user19154568
user19154568

Reputation:

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>

Upvotes: 3

Related Questions