2

I am importing SVG with webpack Raw-loader. It means I want to embed the actual SVG XML inside my markup, as it is.

I am trying to do the following:

<template>
  <div>
    {{svgLoader}}
  </div>
</template>

<script>
import svgLoader from '../assets/loader.svg'
export default {
  data: function () {
    return {
      svgLoader
    }
  }
}
</script>

However, I get the entire SVG XML as pure string and it doesn't get converted to the actual image.

1 Answer 1

2

The SVG won't load because it will be escaped in the mode you are using as it could be a vulnerability. To allow it to render, use the v-html directive which will inject the RAW code allowing it to work.

This should NEVER be used for user generated content, only ever use it for content you trust.

<template>
  <div v-html="svgLoader"></div>
</template>

<script>
import svgLoader from '../assets/loader.svg'
export default {
  data: function () {
    return {
      svgLoader
    }
  }
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

what if I wanted to include inline svg within the template instead of importing something in? like one of these examples via MDN? codepen.io/akinhwan/pen/RQGyYW
In Vue 2.0 you could write a render function to produce an inline svg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.