Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use font awesome 5 on angular material 7

I'm using angular 7.0.1 and angular material 7.0.2, I want to add the font awesome 5.4.2 icons and I'm trying to follow the steps on fontawesome web but I can't get the font awesome icon font.

first:

npm install --save-dev @fortawesome/fontawesome-free

then in styles.scss add:

@import '~@fortawesome/fontawesome-free/scss/fontawesome.scss';

and in _variables.scss add:

$fa-font-path: '~@fortawesome/fontawesome-free/webfonts';

now in app.component.ts add:

import { MatIconRegistry } from "@angular/material";

[...]

constructor(
    public matIconRegistry: MatIconRegistry,
  ) {
    matIconRegistry.registerFontClassAlias ('fas');  // tried with 'fas' and with 'fa'
  }

and finally I'm suposed to print the font awesome icons with:

<mat-icon mat-list-icon fontIcon="fas github"></mat-icon>  // tryed also with 'fas-github', 'fasGithub', 'github', 'fa-github', 'fa github' and 'faGithub'

But the font awesome icon font is never printed. I'm missing something important and obvious but right now I'm not seeing it.

like image 789
Alex Avatar asked Oct 27 '18 18:10

Alex


6 Answers

This is what I've done and it works for me:

In my styles.scss I have

@import "~@fortawesome/fontawesome-free/css/all.css";

Then I have

<mat-icon fontSet="fa" fontIcon="fa-clipboard-check"></mat-icon>

With scss it didnt work for me either.

like image 59
Gabb1995 Avatar answered Oct 21 '22 19:10

Gabb1995


I followed below simple steps, it works for me to install font awesome 5.6.3 (free version) in my Angular 7 project.

run below command to install font-awesome latest version.

npm install --save-dev @fortawesome/fontawesome-free

Add the file paths in the angular.json file.

"styles": ["node_modules/@fortawesome/fontawesome-free/css/all.css"],
"scripts": ["node_modules/@fortawesome/fontawesome-free/js/all.js"]

for reference: https://fontawesome.com/how-to-use/on-the-web/setup/using-package-managers

Tats all.. hope this will help..

like image 42
Karthikeyan Vellingiri Avatar answered Oct 21 '22 20:10

Karthikeyan Vellingiri


If you use scss you can simply import styles to your vendor.scss:

$fa-fort-path: "~@fontawesome/fontawesome-free/webfonts";
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
@import "~@fortawesome/fontawesome-free/scss/regular";
@import "~@fortawesome/fontawesome-free/scss/solid";
@import "~@fortawesome/fontawesome-free/scss/brands";

And use it normally:

<span class="fab fa-refresh fa-spin fa-github" aria-hidden="true"></spin>

Mind the namespaces, fab for brands, fas for solid etc.

That was the best option for me.


Or you can use angular-fontawesome library.

like image 7
Tim Avatar answered Oct 21 '22 20:10

Tim


I use this two solutions:

Large amount of used icons

Import main fontawesome.scss file.

Import preferred style file, e.g. regular.scss (both in angular.json or via @import).

Register default font set in module, so you don't have to define it for each icon:

constructor(private matIconRegistry: MatIconRegistry) {
    matIconRegistry.setDefaultFontSetClass('far'); // or 'fas' for solid style etc.
}

Finally define icon line this:

<mat-icon fontIcon="fa-smile"></mat-icon>

Small amount of used icons

It is not necessary to import all icons, but you only need to define each icon separately in module. Other advantage are that you can define you own icon name for better clarity, combine icons with different styles (solid, duotones...) without loading another icon set, or simply add your own svg icons or logo.

constructor(private matIconRegistry: MatIconRegistry,
            private sanitizer: DomSanitizer) {
    matIconRegistry.addSvgIcon('smile', sanitizer.bypassSecurityTrustResourceUrl('smile.svg'));
}

Icon:

<mat-icon svgIcon="smile"></mat-icon>
like image 3
hovado Avatar answered Oct 21 '22 21:10

hovado


    <fa-icon icon="fa-clipboard-check"></fa-icon>

Make sure FontAwesomeModule is imported in the container module of your component.

In case you need svg:

    import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
    import { library } from '@fortawesome/fontawesome-svg-core';
    import {faClipboardCheck} from '@fortawesome/free-solid-svg-icons';
    library.add(faClipboardCheck);
like image 2
Wildhammer Avatar answered Oct 21 '22 19:10

Wildhammer


If You want to use Font Awesome icons inside <mat-icon> Just use as bellow,

<mat-icon><i class="fa fa-file-chart-line"></i></mat-icon>

** You should have imported the Font Awesome CSS or SCSS to you project

like image 1
Rajitha Kithuldeniya Avatar answered Oct 21 '22 20:10

Rajitha Kithuldeniya