Vue component loads an SVG source dynamically and inline <svg>
so you can manipulate the style of it with CSS or JS.
It looks like basic <img>
so you markup will not be bloated with SVG content.
Loaded SVGs are cached so it will not make network request twice.
npm install vue-inline-svg
Register locally in your component
import InlineSvg from 'vue-inline-svg';
// Your component
export default {
components: {
InlineSvg,
}
}
Or register globally in the root Vue instance
import Vue from 'vue';
// as a plugin
import {InlineSvgPlugin} from 'vue-inline-svg';
Vue.use(InlineSvgPlugin);
// or as a component
import InlineSvg from 'vue-inline-svg';
Vue.component('inline-svg', InlineSvg);
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- Include the `vue-inline-svg` script on your page after Vue script -->
<script src="https://unpkg.com/vue-inline-svg"></script>
<script>
// Register as a plugin
Vue.use(VueInlineSvg.InlineSvgPlugin);
// or as a component
Vue.component('inline-svg', VueInlineSvg.InlineSvgComponent);
new Vue({
// ...
});
</script>
<inline-svg
src="image.svg"
@loaded="svgLoaded()"
@unloaded="svgUnloaded()"
@error="svgLoadError()"
width="150"
height="150"
fill="black"
aria-label="My image"
></inline-svg>
path to SVG file
Note: if you use vue-loader assets or vue-cli, then paths like '../assets/my.svg' will not be handled by file-loader automatically like vue-cli do for <img>
tag, so you will need to use it with require
:
<inline-svg :src="require('../assets/my.svg')"/>
Learn more:
- https://vue-loader.vuejs.org/guide/asset-url.html#transform-rules
- https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling
Other attributes will be passed to inlined <svg>
. Except attributes with false
or null
value.
called when image is loaded and inlined
called when src
prop was changed and another svg start loading
called when svg failed to load
- This module:
vue-simple-svg
: , does not cache network requests, has wrapper around svg, attrs passed to<svg>
are limited, converts<style>
tag intostyle=""
attr
MIT License