Angular CLI 中未显示字体超棒的图标:8.2.2 而不是显示正方形

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

我已经通过 npm install --save font-awesome angular-font-awesomehttps://www.npmjs.com/package/angular-font-awesome 安装了 Font awesome。

链接在 angular.json

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

在 index.html 我已经链接了库

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

我的 html 图标代码如下所示:

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

仍然显示方块而不是图标。

有人能帮忙吗?

我认为您没有在主模块中导入 FontAwesome 模块:

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

编辑

由于您在 angular.json 中添加了 css,因此您不需要在 index.html 中添加 <link> 标签。

要安装 «font-awesome» 使用:

$ npm install @fortawesome/angular-fontawesome@<version>
$ npm install @fortawesome/fontawesome-svg-core
$ npm install @fortawesome/free-solid-svg-icons

然后我们需要在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;
}

更新:

可以通过添加 faFacebook 图标来使用 Facebook fontawesome。让我举个例子。

安装包free-regular-svg-icons:

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

安装包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 { faWhosebug, 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, faWhosebug, 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.

您使用的库不再受支持。楼主推荐用这个: https://github.com/FortAwesome/angular-fontawesome