Vue SVG Icon Component Tool

3.3.2 · maintenance · verified Sun Apr 19

vue-svgicon is a build-time utility for Vue 2.x projects that converts raw SVG files into optimized Vue components. It provides both a command-line interface (`vsvg`) and a programmatic API to automate the generation process, supporting features like custom icon templates, ES6 module output, and integration with SVGO for further optimization of SVG assets. The current stable version for Vue 2 applications is 3.3.2. While this specific package targets Vue 2, the project has evolved into the `@yzfe/vue-svgicon` package for Vue 3 compatibility, which is actively maintained. The core differentiator is its approach of pre-compiling SVGs into Vue components, which can lead to better performance by embedding optimized SVGs directly into the application bundle rather than loading them at runtime, offering a distinct advantage over runtime SVG loaders.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to register the `vue-svgicon` component in a Vue 2 application, include essential global CSS for styling, and use a generated SVG icon within a component. It also provides the necessary command-line steps to generate a sample 'vue.svg' icon from a source SVG file.

/* src/main.js */
import Vue from 'vue'
import App from './App.vue'
import SvgIcon from 'vue-svgicon'

// Register the SvgIcon component globally
Vue.use(SvgIcon, {
  tagName: 'svgicon' // default tag name
})

// Inject global CSS for svg-icon styling (highly recommended).
// In a real project, this would typically be in a global CSS file or imported via a bundler.
const globalSvgIconCss = `
.svg-icon {
    display: inline-block;
    width: 16px;
    height: 16px;
    color: inherit;
    vertical-align: middle;
    fill: none;
    stroke: currentColor;
}
.svg-fill {
    fill: currentColor;
    stroke: none;
}
`;
const style = document.createElement('style');
style.textContent = globalSvgIconCss;
document.head.appendChild(style);

new Vue({
  el: '#app',
  render: h => h(App)
})

/* App.vue */
<template>
  <div id="app">
    <h1>My App</h1>
    <!-- The 'vue' icon must be generated first and imported -->
    <svgicon name="vue" width="50" height="50" color="#42b983 #35495e"></svgicon>
    <p>See console for icon generation instructions.</p>
  </div>
</template>

<script>
// Make sure 'icons/vue' path matches your generated icon directory.
// This assumes 'src/icons' is where 'vsvg' outputs files, and you have configured Webpack
// or similar to resolve 'icons' as an alias to 'src/icons'.
import 'icons/vue' 

export default {
  name: 'App',
}
</script>


// --- Icon Generation (run these commands in your project root before dev server) ---
// 1. Install vue-svgicon globally for the `vsvg` command:
// npm install -g vue-svgicon 
// 
// 2. Create a directory for your source SVGs (e.g., static/svg/src):
// mkdir -p static/svg/src 
// 
// 3. Create a sample SVG file (e.g., static/svg/src/vue.svg):
// echo '<svg viewBox="0 0 200 200"><circle cx="100" cy="100" r="80" fill="#42b983" /></svg>' > static/svg/src/vue.svg
// 
// 4. Generate the icon components into your target directory (e.g., src/icons):
// vsvg -s ./static/svg/src -t ./src/icons 

view raw JSON →