-2

i want import image file from specific link into my vue variable can someone help me i tried with required but it doesn't work this.imagefile = require(linkofimage) does anyone know how to solve it

  • Does this answer your question? [How to import and use image in a Vue single file component?](https://stackoverflow.com/questions/45116796/how-to-import-and-use-image-in-a-vue-single-file-component) – Jordi Nebot Jul 07 '20 at 14:54
  • no i want import the image file directly in variable – Mahdi Suissi Jul 07 '20 at 14:58
  • Do you mean the actual image content? What are you trying to do here? – str Jul 07 '20 at 15:04
  • yes i want get the actual image content i want get the actual image content and im going to convert it in base 64 – Mahdi Suissi Jul 07 '20 at 15:07

1 Answers1

0

I suggest you to import first the images as constants and assign them to your vue data properties in hooks or methods:

<template>
  <div>
    <img :src="imageDirect" alt="alert">
    <img :src="imageOnHook" alt="alert">
    <img :src="imageOnMethod" alt="alert">
    <img :src="imageRequire" alt="alert">
  </div>
</template>

<script>
const image = require('@/assets/alert_logo_card.png')
export default {
  data: () => ({
    imageDirect: image,
    imageOnHook: null,
    imageOnMethod: null,
    imageRequire: null,
  }),
  mounted() {
    this.imageOnHook = image
    this.imageRequire = require('@/assets/alert_logo_card.png')
    this.assignImage()
  },
  methods: {
    assignImage() {
      this.imageOnMethod = this.imageDirect
    }
  }
}
</script>

I'm using the same image just for example purpose.

Something like this also will work:

methods: {
  assignImage() {
    this.imageOnMethod = this.imageDirect
  }
}

Showing an image from the network:

<template>
  <div>
    <img :src="imageFromUrl" alt="alert" width="200" height="200">
  </div>
</template>
<script>
export default {
  data: () => ({
    imageFromUrl: null
  }),
  mounted() {
    setTimeout(() => {
      this.requestImage()
    }, 2000);
  },
  methods: {
    requestImage() {
      const responseFromNetwork = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Google_Photos_icon_%282020%29.svg/1024px-Google_Photos_icon_%282020%29.svg.png'
      this.imageFromUrl = responseFromNetwork
    }
  }
}
</script>
Andres Foronda
  • 1,379
  • 1
  • 8
  • 13