Created
August 25, 2019 19:32
-
-
Save imacrayon/f72ce5e40ec30d047b5eda0ae6ea0e39 to your computer and use it in GitHub Desktop.
Inline SVG Vue Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<svg v-bind="attributes" v-html="html"></svg> | |
</template> | |
<script> | |
export default { | |
props: ['name'], | |
data: () => ({ | |
attributes: {}, | |
html: '', | |
}), | |
created() { | |
const div = document.createElement('div') | |
div.innerHTML = require('../../' + this.name + '.svg') | |
const fragment = document.createDocumentFragment() | |
fragment.appendChild(div) | |
const svg = fragment.querySelector('svg') | |
const attributes = Array.from(svg.attributes).reduce((attrs, attr) => { | |
attrs[attr.name] = attr.value | |
return attrs | |
}, {}) | |
this.attributes = Object.assign(attributes, this.$attrs) | |
this.html = svg.innerHTML | |
}, | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment