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?
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With