Vite 2.x plugin to load SVG files as Vue components.
<template>
<MyIcon />
</template>
<script setup>
import MyIcon from './my-icon.svg'
</script>
npm install vite-svg-loader --save-dev
import svgLoader from 'vite-svg-loader'
export default {
plugins: [vue(), svgLoader()]
}
If you use the loader in a Typescript project, you might get warnings like Type 'string' is not assignable to type 'Component'
.
To fix this you'll need to add the following type definitions to your compiler options:
{
"compilerOptions": {
"types": ["vite-svg-loader", "vite/client"]
}
}