Vue 3 Icon Component
Vue3 Icon is a dedicated Vue 3 component designed for effortlessly rendering SVG path-based icons within modern Vue applications. Currently stable at version 3.0.3, the library receives updates to maintain compatibility with the latest Vue releases and address evolving development practices. Its release cadence is moderate, typically involving major version bumps for significant architectural changes or new features, such as the shift to an ESM-first architecture in v3.0.0 and the adoption of Vite for its build process in v2.0.0. A key differentiator is its versatility, supporting icon data from popular libraries like Material Design Icons (`@mdi/js`), various Font Awesome packages, Simple Icons, and even custom SVG paths. It offers flexible props for controlling icon size, color, rotation, and custom viewbox settings, providing developers with granular control over icon presentation without bundling entire icon font libraries.
Common errors
-
Module not found: Can't resolve 'vue3-icon'
cause Attempting to `require('vue3-icon')` in a CommonJS environment after v3.0.0, or module resolver misconfiguration.fixSwitch to `import SvgIcon from 'vue3-icon'` and ensure your project's build setup supports ESM. For Node.js, ensure `"type": "module"` is set in `package.json` or use an appropriate transpiler. -
ReferenceError: SvgIcon is not defined
cause The `SvgIcon` component was not imported or registered globally/locally in the Vue application.fixImport `SvgIcon` from 'vue3-icon' (e.g., `import SvgIcon from 'vue3-icon';`) and either register it globally (`app.component('svg-icon', SvgIcon)`) or locally within your component's `components` option. -
Cannot read properties of undefined (reading 'path')
cause A FontAwesome icon object (which contains an SVG path and other metadata) was passed to the `path` prop instead of the `fa-icon` prop.fixIf using a FontAwesome icon, use the `:fa-icon` prop: `<svg-icon :fa-icon="faCoffee"></svg-icon>`. The `path` prop expects a simple SVG path string.
Warnings
- breaking Version 3.0.0 shifted the package to an ESM-first architecture. Projects using CommonJS `require()` might encounter module resolution errors.
- breaking Version 2.0.0 changed the library's build system to Vite. Direct imports to specific files within the `dist` directory might require updating file paths.
- gotcha The `vue3-icon` component only provides the rendering logic, not the icon data itself. Users must install separate icon packages (e.g., `@mdi/js`, `@fortawesome/free-solid-svg-icons`, `simple-icons`) to obtain icon paths or objects.
- gotcha FontAwesome icons require the `fa-icon` prop, while other SVG paths use the `path` prop. Using the wrong prop will result in the icon not rendering correctly or throwing an error.
Install
-
npm install vue3-icon -
yarn add vue3-icon -
pnpm add vue3-icon
Imports
- SvgIcon
const SvgIcon = require('vue3-icon')import SvgIcon from 'vue3-icon'
- mdiAccount
import { mdiAccount } from '@mdi/js' - faCoffee
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
Quickstart
import { createApp } from 'vue';
import SvgIcon from 'vue3-icon';
import { mdiAccount } from '@mdi/js';
const app = createApp({
setup() {
const myCustomIcon = "M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z";
return {
mdiAccount,
myCustomIcon
};
},
template: `
<div id="app">
<h1>Vue3 Icon Example</h1>
<p>Material Design Icon:</p>
<svg-icon type="mdi" :path="mdiAccount" :size="48" style="color: dodgerblue;"></svg-icon>
<p>Custom SVG Path Icon (with custom viewbox and color):</p>
<svg-icon :path="myCustomIcon" size="24" viewbox="0 0 24 24" style="color: green;"></svg-icon>
</div>
`,
});
app.component("svg-icon", SvgIcon);
app.mount("#app");