import Vue from'vue'import{ library, config }from'@fortawesome/fontawesome-svg-core'import{ FontAwesomeIcon }from'@fortawesome/vue-fontawesome'import{ fas }from'@fortawesome/free-solid-svg-icons'// This is important, we are going to let Nuxt.js worry about the CSS
config.autoAddCss =false// You can add your icons directly in this plugin. See other examples for how you// can add other styles or just individual icons.
library.add(fas)// Register the component globally
Vue.component('font-awesome-icon', FontAwesomeIcon)
Modify nuxt.config.js adding to the `css` and `plugins` sections.css:['@fortawesome/fontawesome-svg-core/styles.css']plugins:['~/plugins/fontawesome.js']
PurgeCSS
If you use PurgeCSS(opens new window) - or use the nuxt.js tailwindcss module which comes with PurgeCSS prebundled - you need to add fontawesome CSS classes to the whitelist, Otherwise, PurgeCSS will treat them as unused and remove them since the classes only get inserted on rendering.
In your nuxt.config.js, add a purgeCSS config object. You may adjust the regex to your liking:
purgeCSS:{whitelistPatterns:[/svg.*/,/fa.*/]},
Advertisement
Remove ads with a Pro plan!
Using this requires Font Awesome Pro
Pro
A subscription to a Pro-level plan will remove all third-party advertisments on fontawesome.com.
And of course Pro-level plans come with…
All 16,083 Icons in Font Awesome
Solid, Regular, Light, Thin, and Duotone Styles for Each Icon + Brands
A Perpetual License to Use Pro
Services and Tools to Make Easy Work of Using Icons
This project and all Font Awesome SVG icons will work just fine in these components but we need to take an additional step to add the CSS correctly.
To take advantage of encapsulation that the Shadow DOM provides and to keep other areas of the DOM clean we need to add the Font Awesome CSS to the root of the Shadow DOM.
Here is an example that leverages the mounted() lifecycle hook to insert the CSS.
<script>import{ config, dom }from'@fortawesome/fontawesome-svg-core'import{ faCoffee, faStroopwafel, faDragon }from'@fortawesome/free-solid-svg-icons'import{ FontAwesomeIcon }from'@fortawesome/vue-fontawesome'// Make sure you tell Font Awesome to skip auto-inserting CSS into the <head>
config.autoAddCss =falseconst component ={name:'MyCustomElement',template:`<font-awesome-icon :icon="icon" />`,components:{
FontAwesomeIcon
},mounted(){// This will only work on your root Vue component since it's using $parentconst{ shadowRoot }=this.$parent.$options
const id ='fa-styles'if(!shadowRoot.getElementById(`${id}`)){const faStyles = document.createElement('style')
faStyles.setAttribute('id', id)
faStyles.textContent = dom.css()
shadowRoot.appendChild(faStyles)}},computed:{icon(){const icons =[faCoffee, faStroopwafel, faDragon]return icons[Math.floor(Math.random()*3)]}}}exportdefault component
</script>