Font-awsome icons not showing in Angular CLI: 8.2.2 instead showing squares

回眸只為那壹抹淺笑 提交于 2019-12-24 12:41:54

问题


I have installed Font awesome via npm install --save font-awesome angular-font-awesome from https://www.npmjs.com/package/angular-font-awesome.

Linked it in angular.json

"styles": [
     "src/styles.css",
     "node_modules/font-awesome/css/font-awesome.min.css"
]

in index.html i have linked the library

<link rel="stylesheet" type="text/css" href="../node_modules/font-awesome/css/font-awesome.min.css">

my html code for the icon looks like this:

<i class="fa fa-facebook-f"></i>

and still squares are showed instead of icons.

Can anyone help?


回答1:


To install «font-awesome» use:

$ npm install @fortawesome/fontawesome-svg-core
$ npm install @fortawesome/free-solid-svg-icons

Then we need to import this in app.module.ts:

imports: [
    BrowserModule,
    FontAwesomeModule
],

src/app/app.component.html:

<div style="text-align:center">
  <fa-icon [icon]="faCoffee"></fa-icon>
</div>

src/app/app.component.ts

import { Component } from '@angular/core';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  faCoffee = faCoffee;
}

UPDATE:

It is possible to use Facebook fontawesome by adding faFacebook icon. Let me show an example.

To install package free-regular-svg-icons:

npm i --save @fortawesome/free-regular-svg-icons

To install package free-regular-svg-icons:

npm i --save @fortawesome/free-brands-svg-icon

App.module.ts:

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

import { FontAwesomeModule, FaIconLibrary } from '@fortawesome/angular-fontawesome';
import { faSquare, faCheckSquare } from '@fortawesome/free-solid-svg-icons';
import { faSquare as farSquare, faCheckSquare as farCheckSquare } from '@fortawesome/free-regular-svg-icons';
import { faStackOverflow, faGithub, faMedium, faFacebook } from '@fortawesome/free-brands-svg-icons';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';

@NgModule({
  imports:      [ BrowserModule, FormsModule, FontAwesomeModule ],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule {
  constructor(private library: FaIconLibrary) {
    library.addIcons(faSquare, faCheckSquare, farSquare
        , farCheckSquare, faStackOverflow, faGithub, faMedium, faFacebook);
  }
}

app.component.html:

<div style="text-align:center">
  <h2>Using Brand Icons</h2>
  <fa-icon [icon]="['fab', 'stack-overflow']"></fa-icon>
  <br>
  <fa-icon [icon]="['fab', 'github']"></fa-icon>
  <br>
  <fa-icon [icon]="['fab', 'medium']"></fa-icon>
  <br>
  <fa-icon [icon]="['fab', 'facebook']"></fa-icon>  
  <br>
</div>

It can be seen at stackblitz.com.




回答2:


The library you use isn't supported anymore. It's recommended by the owner to use this one: https://github.com/FortAwesome/angular-fontawesome




回答3:


I think you didn't import the FontAwesome module in your main module:

import { AngularFontAwesomeModule } from 'angular-font-awesome';
@NgModule({
  //...
  imports: [
    //...
    AngularFontAwesomeModule
  ],
  //...
})
export class AppModule { }

EDIT

Since you added the css in your angular.json you shouldn't need to add the <link> tag in index.html.



来源:https://stackoverflow.com/questions/57670433/font-awsome-icons-not-showing-in-angular-cli-8-2-2-instead-showing-squares

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!