Another mechanism that the SVG Core provides is a JavaScript class called Library.
With a subsetted Kit, this can be an easy way to add all icons once and use them with a syntax that requires less typing.
Now all icons in the Kit have been added in just one, easy line. No fuss, no muss.
Using it doesn’t require importing the icons. You just need an array or string.
Also note here that we’ve switched from importing and using FontAwesomeIcon
directly and are using the already registered component, through
app.component(). That means our syntax shifts slightly and we use
<font-awesome-icon ...> now.
If you can’t or don’t want to use a Kit, you can explicitly add individual icons to each component. Here’s a simple example:
Set up the library
You’ll first create a library of all the icons you want to use in your project in the src/main.js or src/main.ts
file. Here’s an example that adds the Solid style User Secret icon to the library:
You can now call the icons. You can add icons to your Vue 3 or Vue 2 project using a string format or an array
format. Just use the syntax below wherever you want the icons to appear in your project, like in the src/App.vue file.
Here’s the rest of the example we started above that adds the fa-user-secret icon into the app UI:
Here’s an example with a number of icons in different styles, just to give you a sense of how the syntax changes from
style to style in your main.js file.
In our call to library.add() we’re passing:
fas: which represents all of the icons in @fortawesome/free-solid-svg-icons. (Be careful importing whole styles - it can be a LOT of icons!) So any of the icons in that package may be referenced by icon name as a string anywhere else in our app. For example: coffee, check-square, or spinner.
fass: represents all sharp solid icons in @sharp-solid-svg-icons. (pro icons!)
fad: represents all duotone solid icons in @pro-duotone-svg-icons. (pro icons!)
fadt: represents all duotone thin icons in @duotone-thin-svg-icons. (pro icons!)
fasds: represents all sharp duotone solid icons in @sharp-duotone-solid-svg-icons. (pro icons!)
faTwitter, faFontAwesome, faHatCowboy, and faHatChef: Adding each of these icons individually allows us to refer to them throughout our app by their icon string names, twitter, font-awesome, hat-cowboy, hat-chef, and plate-utensils.
You can then use any of those icons anywhere in your app without needing to re-import into each component. So if you used icons in a couple of components, that would end up looking something like this:
You’ll notice we were able use the imported brand icons without explicitly importing them in the component. And we used the dog, hippo, feather, fish, and dolphin icons without explicitly importing them anywhere. But, each bundle now has over 1000 solid icons plus the two brand icons we added, which is more than we’re using - a good reason to avoid importing a whole style.
Same icons, Different Styles
Using ES modules and import statements we can define unique names for two or more different styles of the same icon.
Here’s is what your main.js file would look like:
If you are using inline templates or HTML as templates you need to be careful to avoid self-closing tags. Read more
about self-closing tags on Vue.js. If
you are writing these types of templates, you’ll need to adjust the syntax to be valid HTML, like this: