Angular 9 + FontAwesome 0.6 : "Error NG8001: ' fa-icon' is not a known element"

标签 angular font-awesome

我在尝试 时遇到问题使用 fontawesome 图标 .我已经在我的项目中安装了带有命令行的 FontAwesome:

ng add @fortawesome/angular-fontawesome@latest

我有一个子模块,我想在其中使用“fas”->“images”图标。所以,我创建了我的子模块:
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);
  }
}

我在这个子模块中有一个组件(photo-list.component.ts 和 photo-list.component.html)。在它的 HTML 文件中,我只是把这一行放在我的标题中显示图标:
<h1><fa-icon [icon]="['fas','images']"></fa-icon> Images</h1>

当我运行我的 Angular 项目并打开此模块时,出现以下错误(并且图标确实显示):错误 NG8001:'fa-icon' 不是已知元素

为什么它不起作用?

最佳答案

我检查了代码:fontawesome 发生了一些变化,现在必须正确导入:

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);
  }
}

根据您的代码查看工作示例:https://stackblitz.com/edit/angular-5qu1fy

关于Angular 9 + FontAwesome 0.6 : "Error NG8001: ' fa-icon' is not a known element",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61291727/

相关文章:

javascript - 显示来自 Web 服务的数据(Js、Angular)

Angular 11 : Unit testing an HttpInterceptor - can't get catchError to work in test

html - CSS3 样式的复选框 - 尝试缩进/左对齐复选框右侧的标签文本

css - 如何将自定义图像添加到 Font Awesome 图标

font-face - “网络错误 : 404 Not Found fontawesome-webfont. woff?v=4.0.3

html - 在加载 <IMG> 标记中的所有图像之前,不应加载 ngOnInit 或 ngAfterViewInit 中的内容

Angular2 Material 映射问题

angular - Angular2 的 enzyme 浅渲染?

html - 如何在 Chrome 中删除 &lt;input type=date> 悬停时的箭头

html - 将 font awesome span 图标定位在 anchor 内的文本上方