Volodymyr Humeniuk
Volodymyr Humeniuk

Reputation: 3791

Import all icons from Fontawesome

I using Fontawesome 5 in my Angular project in this way:

import fontawesome from '@fortawesome/fontawesome';
import { faBold, faItalic, faUnderline } from '@fortawesome/fontawesome-free-solid';

and in contructor:

fontawesome.library.add(faBold, faItalic, faUnderline)

But it's very silly to import each icon separately. Can I somehow import all the icons at once?

upd: import * as icons ... does not work.

Upvotes: 17

Views: 14225

Answers (4)

Arun Kumar A.J
Arun Kumar A.J

Reputation: 171

As in docs here, you can do

import { fas } from '@fortawesome/free-solid-svg-icons';
import { far } from '@fortawesome/free-regular-svg-icons';
import { FontAwesomeModule, FaIconLibrary } from '@fortawesome/angular-fontawesome';

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

export class AppModule {

constructor(library: FaIconLibrary) {
  library.addIconPacks(fas, far);
  }
}

Upvotes: 5

Tore Aurstad
Tore Aurstad

Reputation: 3796

Font Awesome 5 in Angular 8 based appHere is how I did it, I first import the Font awesome packages into the app module:

import { fas } from '@fortawesome/free-solid-svg-icons';
import { far } from '@fortawesome/free-regular-svg-icons';
import { fab } from '@fortawesome/free-brands-svg-icons';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';

Importing the FontAwesomeModule also into the imports section.

Add the following in to your constructor of app module:

 constructor(){
    library.add(fab, far, fas);
  }

Now you can reference the Font Awesome icons from inside any component like in this markup example:

    <div class="crop"
     (click)="onClick()"
     [style.width.px]="starWidth"
     [title]="rating">
  <div style="width: 75px">
    <span><fa-icon [icon]="['far', 'star']"></fa-icon></span>
    <span><fa-icon [icon]="['far', 'star']"></fa-icon></span>
    <span><fa-icon [icon]="['far', 'star']"></fa-icon></span>
    <span><fa-icon [icon]="['far', 'star']"></fa-icon></span>
    <span><fa-icon [icon]="['far', 'star']"></fa-icon></span>
  </div>
</div>

Note that if you do not use the solid icons from the 'fas' library, you must specify the type of Font Awesome icon library, such as 'far' for the regular icons.

I ended up with using the following npm packages: "@fortawesome/angular-fontawesome": "^0.3.0", "@fortawesome/fontawesome-svg-core": "^1.2.21", "@fortawesome/free-brands-svg-icons": "^5.10.1", "@fortawesome/free-regular-svg-icons": "^5.10.1", "@fortawesome/free-solid-svg-icons": "^5.10.1",

Note: I did a downgrade to version 0.3.0 of the angular-fontawesome package.

Tested out in Angular 8.

Upvotes: 3

sr9yar
sr9yar

Reputation: 5310

import { fas } from '@fortawesome/fontawesome-free-solid';

and then

fontawesome.library.add(fas)

same for other styles

import { fab } from '@fortawesome/fontawesome-free-brands';
import { far } from '@fortawesome/fontawesome-free-regular';
...
fontawesome.library.add( fab, far );

Upvotes: 27

August
August

Reputation: 2113

Why not loading the font as asset (adding the files to assets folder and defining the font in css file)? Then you have all characters (icons) available to use.

Upvotes: 2

Related Questions