I added
"@fortawesome/fontawesome-free": "5.15.1"
to dependencies
in my package.json
and then I added to my html
<span class="fas fa-sign-in-alt"></span>
but nothing shows up.
What do I need to do? It’s completely unclear.
I’m in a project created by VisualStudio and it has Twitter Bootstrap in the package.json
and that appears to be working without bootstrap being mentioned anywhere else, so why doesn’t it work for FontAwesome?
This doesn’t work.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [
'../../node_modules/@fortawesome/fontawesome-free/css/all.min.css'
]
})
export class AppComponent {
title = 'app';
}
Adding to styles.css
:
@import "../node_modules/@fortawesome/fontawesome-free/css/fontawesome.min.css";
also doesn’t work. This is crap.
What was so bad about adding a <link>
in the <head>
? This is awful.
2
Answers
Adding third-party global styles to angular.json
In your angular.json file you’ll have a property that is meant to describe files or globs to include in your webpack build.
So, for your case of font-awesome, it would look something like the following:
To get that file, download the corresponding fortawesome-package as described on their website :
Once you’ve modified your
angular.json
you will have to restart your angular cli for the changes to take effect.Why can you not add it directly to
index.html
‘s head section.The reason for this is that the index.html you will distribute is being generated by webpack once you run
ng build
so you have to take this extra step in telling the webpack toolchain to include your style in the generated index.html’s head.More information
For more information on Angular workspace configuraiton you can check the reference guide here: https://angular.io/guide/workspace-config
For use with Angular 9+
You can also consider using the
angular-fontawesome
librarySteps
Using
npm
FontAwesomeModule
to imports in src/app/app.module.ts: