【问题标题】:How can I use a font awesome brand icon in an angular component?如何在角度组件中使用字体真棒品牌图标?
【发布时间】:2020-08-22 16:19:13
【问题描述】:

我正在尝试在我的 Angular 10 应用程序中使用 Font Awesome 品牌图标。 我已经安装了 fontawesome 并尝试使用以下方法导入它:

import { faDiscord } from '@fortawesome/fontawesome-free-brands';

在我的组件中。 然后我将 faDiscord 分配给一个属性,并尝试在我的 html 中像这样使用它:

<fa-icon [icon]="faDiscord"></fa-icon>

但是,我的组件现在卡在加载中,我该如何解决这个问题?已经看到这个问题了:How do i use brand fontawesome icon in angular

【问题讨论】:

    标签: angular font-awesome font-awesome-5 angular-fontawesome


    【解决方案1】:

    首先,运行以下命令:

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

    您应该根据您的 Angular 版本更改最后一个命令中的版本号。 See the docs.

    Font Awesome 图标被分成不同的样式,这些样式在 单独的包装以满足不同的需求并减少个体 包装尺寸。要使用图标,您需要安装一个包 包含它。

    在您的情况下,您正在尝试使用 icon ,它在 Brands Style 包中可用。 Discord Icon

    所以运行以下命令: See Here.

    npm install @fortawesome/free-brands-svg-icons
    

    现在在 AppModule 中导入 FontAwesomeModule

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    
    import { AppComponent } from './app.component';
    import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
    
    @NgModule({
      imports: [
        BrowserModule,
        FontAwesomeModule
      ],
      declarations: [AppComponent],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    在您的组件blabla.components.ts 中,创建一个新变量并将faDiscord 分配给它:

    import { Component } from '@angular/core';
    import { faDiscord } from '@fortawesome/fontawesome-free-brands';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      faDiscord  = faDiscord;
    }
    

    现在在您的app.component.html

    <fa-icon [icon]="faDiscord"></fa-icon>
    

    这是Stackblitz

    【讨论】:

    • 你添加FontAwesomeModule到你的应用模块了吗?
    • 在您的 app.module.ts 文件中,将 FontAwesomeModule 添加到导入部分。导入:[ BrowserModule,FontAwesomeModule ],
    • 你的 Angular 版本是什么?
    • 拜托,如果我的回答有帮助,你能接受吗?
    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 2023-02-10
    • 2018-06-14
    • 2022-08-09
    • 2020-11-14
    • 1970-01-01
    • 2014-01-16
    • 2020-03-26
    相关资源
    最近更新 更多