Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import and use image in a Vue single file component?

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?

like image 913
aks Avatar asked Jul 15 '17 09:07

aks


People also ask

How do I import custom components to Vue?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.

What are Vue single file components?

Single File Components allow us to define the HTML/CSS and JS of a component all within a single . vue file. A single-file component is composed of three parts: The <template> section which contains the component's markup in plain HTML.


1 Answers

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>  
like image 97
papey Avatar answered Oct 04 '22 08:10

papey