albert_anthony6
albert_anthony6

Reputation: 614

Vue.js how to import SVG logo

I'm new to vue.js and am used to React. I'm currently trying to import an SVG logo into my header component but I'm not sure how. In react, I would simply do import Logo from './path; and use Logo wherever I needed it within the current component. This is basically what I'm attempting to do right now but I keep getting errors. Could anyone tell me how this could be done in Vue.js?

<template>
    <header class="nav">
        <img src={Logo} alt="24G Logo">
    </header>
</template>

<script>
    import Logo from '../assets/76_logo.svg';

    export default {
        name: 'Header'
    }
</script>

<style lang="scss" scoped>

</style>

Upvotes: 3

Views: 16064

Answers (4)

kian wigin
kian wigin

Reputation: 1

edit your code

<script>
import Logo from '../assets/76_logo.svg';

export default {
    name: 'Header',
    data(){
      Logo: Logo
    }
}

Upvotes: 0

Surya
Surya

Reputation: 15992

Here is another approach that I used:

<template>
  <a href="#"
    class="log-link-css-class">
    <!-- SVG Icon Start-->
    <img alt="alt message" class="your-logo-css-class"
          src="@/assets/images/logofilename.svg">
    <!-- <SVG Icon End /> -->
  </a>
</template>

No import required. Vue automatically converts it to the unique URL.

Upvotes: 1

Alan Spurlock
Alan Spurlock

Reputation: 343

After searching and searching, and seeing all the answers were old, I went ahead and tried the newish v-html prop.

The result, success! <div v-html="avatar" style="width: 100%"></div>

The avatar is a full element that I stored in the database. No loaders, no imports, just using the built in resources of Vue.js

If you leave out the style, then the svg will not show.

Also, loading the full element enables me to attach a ref prop to the element. Enabling me to access the svg through script.

Hope that helps someone!!
I used this with avataaar's random avatar generator and stored the resulting svg to the database (mongo)

Upvotes: 2

n-smits
n-smits

Reputation: 713

Here are three options. The best in my opinion is the third:

  1. Simply input src like in any webpage <img src='../path/to/file.svg' ... though that come with some drawbacks (regardless if it's :src='logoPath' where logoPath is variable containing the same. For a short overview see this stack answer, and for more details see this article from css tricks.

  2. Check out svg-vue-loader. Vue won't automatically import svg without a loader.

  3. Just paste it in! (Open the svg file and copy paste it into the template.) The best option in my opinion, especially when prototyping or for smaller projects. Like so:

<template>
  <header class="nav">
    <svg ....
  </header>
</template>

If that would make it too crowded later on, just make a new component, call it say Logo, and paste svg in there and then import MainLogo component into your header.

No need for svg-loaders. Though loaders are a dev dependency, so not like it would cost you anyway; they would just do the same thing you can do manually.

// in MainLogo.vue

<template>
  <svg ....
</template>
// in MainHeader.vue

<template>
  <header class="nav">
    <MainLogo>
  </header>
</template>

<script>
  import MainLogo from '../path/to/file.vue'
  export default {
    components: { MainLogo }
  }
</script>

Cheers

Upvotes: 5

Related Questions