82

I think this should be simple, but I am facing some trouble on how to import and use an image in Vue single file component. Can someone help me how to do this? Here is my code snippet:

<template lang="html">
    <img src="zapierLogo" />
</template>
    
<script>
    import zapierLogo from 'images/zapier_logo.svg'
    
    export default {
    }
</script>
    
<style lang="css">
</style>

I have tried using :src, src="{{ zapierLogo }}", etc. But nothing seems to work. I was not able to find any example too. Any help?

yohlu
  • 5
  • 4
aks
  • 6,072
  • 4
  • 43
  • 68

6 Answers6

141

As simple as:

<template>
    <div id="app">
        <img src="./assets/logo.png">
    </div>
</template>
    
<script>
    export default {
    }
</script>
    
<style lang="css">
</style> 

Taken from the project generated by vue cli.

If you want to use your image as a module, do not forget to bind data to your Vuejs component:

<template>
    <div id="app">
        <img :src="image"/>
    </div>
</template>
    
<script>
    import image from "./assets/logo.png"
    
    export default {
        data: function () {
            return {
                image: image
            }
        }
    }
</script>
    
<style lang="css">
</style>

And a shorter version:

<template>
    <div id="app">
        <img :src="require('./assets/logo.png')"/>
    </div>
</template>
    
<script>
    export default {
    }
</script>
    
<style lang="css">
</style> 
yohlu
  • 5
  • 4
papey
  • 3,218
  • 1
  • 15
  • 18
22

It is heavily suggested to make use of webpack when importing pictures from assets and in general for optimisation and pathing purposes

If you wish to load them by webpack you can simply use :src='require('path/to/file')' Make sure you use : otherwise it won't execute the require statement as Javascript.

In typescript you can do almost the exact same operation: :src="require('@/assets/image.png')"

Why the following is generally considered bad practice:

<template>
  <div id="app">
    <img src="./assets/logo.png">
  </div>
</template>

<script>
export default {
}
</script>

<style lang="scss">
</style> 

When building using the Vue cli, webpack is not able to ensure that the assets file will maintain a structure that follows the relative importing. This is due to webpack trying to optimize and chunk items appearing inside of the assets folder. If you wish to use a relative import you should do so from within the static folder and use: <img src="./static/logo.png">

li x
  • 3,392
  • 2
  • 26
  • 45
  • So ``? This doesn't work for me. It complains about dependency not found. – Pithikos Mar 04 '20 at 10:14
  • @Pithikos There's a lot of reasons why you might be getting this error, can you expand more or maybe post a question and I can try and debug. – li x Mar 05 '20 at 10:37
  • url-loader allows you to conditionally inline a file as base-64 data URL if they are smaller than a given threshold. This can reduce the amount of HTTP requests for trivial files. If the file is larger than the threshold, it automatically falls back to file-loader. - If you don't use webpack to load your image you won't recieve this benefits or the other mentioned here: https://vue-loader.vuejs.org/guide/asset-url.html#transform-rules @akauppi – li x Feb 11 '21 at 11:17
  • @akauppi I highly doubt you're using vue.js but not webpack, vue's native development server is webpack and the builder is also webpack, Also you asked if it's still relevant, as per the documentation it's still very relevant thus answering your comment. Next time don't ask a question if you don't want an answer. – li x Feb 13 '21 at 19:55
7

I came across this issue recently, and i'm using Typescript. If you're using Typescript like I am, then you need to import assets like so:

<img src="@/assets/images/logo.png" alt="">
Simdi
  • 316
  • 2
  • 10
3

You can also use the root shortcut like so

  <template>
   <div class="container">
    <h1>Recipes</h1>
      <img src="@/assets/burger.jpg" />
   </div>
  </template>

Although this was Nuxt, it should be same with Vue CLI.

2

These both work for me in JavaScript and TypeScript

<img src="@/assets/images/logo.png" alt=""> 

or

 <img src="./assets/images/logo.png" alt="">
Shayki Abramczyk
  • 24,285
  • 13
  • 49
  • 65
JustRaman
  • 895
  • 6
  • 9
  • 1
    While this actually does work, you're not taking advantage of webpack in this case. Furthermore this `src="./assets/images/logo.png"` is discouraged in practice as unlike static you can't ensure how items from the assets directory will be bundled when building. – li x Oct 10 '19 at 08:51
1

I encounter a problem in quasar which is a mobile framework based vue, the tidle syntax ~assets/cover.jpg works in normal component, but not in my dynamic defined component, that is defined by

let c=Vue.component('compName',{...})

finally this work:

    computed: {
      coverUri() {
        return require('../assets/cover.jpg');
      }
    }
<q-img class="coverImg" :src="coverUri" :height="uiBook.coverHeight" spinner-color="white"/>

according to the explain at https://quasar.dev/quasar-cli/handling-assets

In *.vue components, all your templates and CSS are parsed by vue-html-loader and css-loader to look for asset URLs. For example, in <img src="./logo.png"> and background: url(./logo.png), "./logo.png" is a relative asset path and will be resolved by Webpack as a module dependency.

towith
  • 29
  • 3