I'm facing a problem trying to use fontawesome icons. I'd already installed FontAwesome with command line into my project:
ng add @fortawesome/angular-fontawesome@latest
I have a submodule and I want to use "fas"->"images" icon just inside it. So, I'd created my submodule:
import { PhotoListComponent } from './photo-list.component';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/free-solid-svg-icons';
@NgModule({
declarations: [
PhotoListComponent
],
imports: [
CommonModule,
FontAwesomeModule
]
})
export class PhotoListModule {
constructor() {
library.add(fas);
}
}
I have a component in this submodule (photo-list.component.ts and photo-list.component.html). In its HTML file I just put this line to show icon in my title:
<h1><fa-icon [icon]="['fas','images']"></fa-icon> Images</h1>
When I run my angular project and open this module, the following error occurs (and icon does note display): Error NG8001: 'fa-icon' is not a known element
Why doesn't it work?
Font Awesome now has an official Angular component that’s available for all who want to easily use our icons in projects. Plan to use the SVG+JS method of Font Awesome with Angular. Have your project files handy to work on. The Font Awesome Angular component is available on npm and that's also where we maintain its official documentation.
In this step, you need to just install font-awesome on your angular 9 and import css file to style.css file. this is only for css importing. so you can run command bellow: After successfully, installed font-awesome, we need to import css files on angular.json file. so let's add following lines on it.
Error NG8001: 'fa-icon' is not a known element. Here's a solution on fixing these errors. Where FontAwesomeModule is only in the imports section. Does the consumer HTML page contain components that are not there?
You've Got Options! The Font Awesome Angular component is a great option for integrating with AngularJS projects, but you can also opt to use Web Fonts with CSS with a Kit or by self-hosting.
I've checked the code: there were some changes in fontawesome and now the correct import has to be next:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { FontAwesomeModule, FaIconLibrary } from '@fortawesome/angular-fontawesome';
import { faCoffee, fas } from '@fortawesome/free-solid-svg-icons';
@NgModule({
imports: [ BrowserModule, FormsModule, FontAwesomeModule ],
declarations: [ AppComponent, HelloComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {
constructor(library: FaIconLibrary) {
library.addIconPacks(fas);
library.addIcons(faCoffee);
}
}
see the working example, based on your code: https://stackblitz.com/edit/angular-5qu1fy
I know this is an old question but if someone is using Angular Material I would like to add some information. As per their documentation https://github.com/FortAwesome/angular-fontawesome. Just import the fontawesome in your material module , and then use it in any components and bind property in html, like;
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
@NgModule({
imports: [FontAwesomeModule]
})
export class MaterialModule {}
//any component
import { faGoogle } from '@fortawesome/free-brands-svg-icons';
googleIcon = faGoogle;
//html
<fa-icon [icon]="googleIcon"></fa-icon>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With