Skip to content
Advertisement

How to import multiple svgs in vue.js via vue-svg-loader

I want to import multiple svg’s in one vue component. The documentation says I Have to import each one of them, but how can I import multiple svgs in a much shorter and cleaner way?

vue-svg-loader documentation: https://vue-svg-loader.js.org/

JavaScript

What happens if I got over one hundred svg’s I want to import?

Any ideas to solve this?

Advertisement

Answer

Create a base component and register it globally since you’ll use it very frequently.


Create a <BaseIcon> component that uses require with expression to create a context for the SVG modules:

JavaScript

Note: We use <Component> to handle dynamic components, which assumes you’ll use vue-svg-loader and the SVGs are treated as components. If that is not the case, use an <img> tag instead and use src instead of is.


Registering the base component globally:

If you’re only creating a single base component, you can just go to your main.js file and before mounting the app do:

JavaScript

Else, if you want something a little bit more complex, take a look at how this boilerplate registers base components automatically.


Finally, use the component like so:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement