How to add an image in VueJS?

Hi Friends đź‘‹,

Welcome To SortoutCode! ❤️

Today, I am going to show you How do you add an image in VueJS?

Table of Content

  • Setup the Vue (Optional)
  • Create FirstComponent.vue and import it into App.js
  • Add image

This article will guide you to how do I add an image in vueJS.

Setup the Vue (Optional)

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation.

How to Install the VueJS project?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. the component name is FirstComponent.vue and imported into the App.js file:

App.js

<template>
  <div id="app">
    <FirstComponent />
  </div>
</template>

<script>
import FirstComponent from "./components/FirstComponent.vue";
export default {
  name: "App",
  components: {
    FirstComponent,
  },
};
</script>

Add image

You can see that image is available in our src/assets folder is logo.png and the other one is d2.jpg image:

Folder Structure

There are three ways to add an image to your project. Let’s see the code:

FirstComponent.js

<template>
  <div class="container-fluid">
    <h1>Welcome to sortoutcode.com</h1>
    <div class="row">
      <div class="col-sm-4">
        <img src="../assets/d2.jpg" alt="Fire Dragon" />
      </div>
      <div class="col-sm-4">
        <img :src="imgdata" alt="Fire Dragon" />
      </div>
      <div class="col-sm-4">
        <img
          src="https://i.picsum.photos/id/41/1024/400.jpg?hmac=2noCYEDtKPIzvN96ROUd1HqJKjGvqfWdf9lRnFA1njQ"
          alt="Fire Dragon"
        />
      </div>
    </div>
  </div>
</template>
<script>
import firedragon from "../assets/d2.jpg";
export default {
  name: "FirstComponent",
  data() {
    return {
      imgdata: firedragon,
      imgdata2: "",
    };
  },
};
</script>
<style lang="stylus" scoped>
div {
  width: auto;
  margin: 0 auto;
}

div img {
  max-width: 100%;
}
</style>

For now, let’s check the output.

Folder Structure

All the best đź‘Ť.

Follow me on Twitter