Vue 3 (Vite) — Inline SVGs as Components

Import SVG as Vue components with Vite and vite-svg-loader

In modern Vue apps, the cleanest way to use icons is to import SVG files as Vue components. In this guide, you’ll export minified SVGs from Axialis IconVectors with currentColor paints, set up vite-svg-loader for Vite, and theme icons via CSS while keeping bundles small.

Export a clean, themable SVG from IconVectors

  1. Open or create your icon:
    • File → Open… (Ctrl+O) or New Icon (Ctrl+N).
    • Set fills/strokes to currentColor so the icon inherits CSS/text color in Vue.
    Preparing a themable SVG in IconVectors
    Export a minified SVG via File → Export → Export Minified (Shift+Ctrl+M) to strip metadata and keep a clean viewBox.

Install & configure vite-svg-loader

  1. Install
    npm i -D vite-svg-loader
    # or: pnpm add -D vite-svg-loader
    # or:  yarn add -D vite-svg-loader
  2. Configure vite.config.ts
    import { defineConfig } from 'vite'
    import vue from '@vitejs/plugin-vue'
    import svgLoader from 'vite-svg-loader'
    
    export default defineConfig({
      plugins: [
        vue(),
        svgLoader({
          // Use SVGO and keep viewBox; strip width/height so CSS controls size
          svgo: true,
          svgoConfig: {
            plugins: [
              { name: 'preset-default', params: { overrides: { removeViewBox: false } } },
              'removeDimensions',
              'prefixIds' // avoid ID collisions if your SVG uses ids/gradients
            ]
          },
          defaultImport: 'component' // allow: import Icon from './icon.svg'
        })
      ]
    })
  3. Add TypeScript types (project root)
    // env.d.ts
    /// <reference types="vite/client" />
    declare module '*.svg' {
      import type { DefineComponent } from 'vue'
      const component: DefineComponent
      export default component
    }
    declare module '*.svg?component' {
      import type { DefineComponent } from 'vue'
      const component: DefineComponent
      export default component
    }

Import SVG as a Vue component & theme with CSS

  1. Inline component import
    <!-- src/components/SaveButton.vue -->
    <script setup lang="ts">
    import CheckIcon from '@/icons/check.svg'  // because defaultImport: 'component'
    </script>
    
    <template>
      <button class="inline-flex items-center gap-2 text-emerald-600 hover:text-emerald-700">
        <CheckIcon class="w-5 h-5" aria-hidden="true" />
        Save
      </button>
    </template>

    Theming: Because your SVG uses currentColor, the icon adopts the button’s text color. Change color with a class or inline style (e.g., style="color:#2563eb").

  2. Alternate import with query
    <script setup lang="ts">
    import CheckIcon from '@/icons/check.svg?component'
    </script>

Project structure & tree‑shaking

Troubleshooting

Start Making SVG Icons Today with IconVectors

Download the fully-functional 30‑Day Free Trial and unlock your icon design workflow.

Version 1.10 - September 17, 2025