angular - 无法使 fa-icon 在 Angular 子模块中工作
问题描述
我需要基于 Angular 12 创建一个非常简单的静态应用程序(“静态”意味着它不使用 AJAX)并显示一些 FontAwesome 图标。我决定早点上传存储库
我做了ng add @fortawesome/angular-fontawesome
并选择了所有图标包。然后我想使用 Angular 路由和组件创建一个隐私政策页面。我以前做过,但我在这里没有成功。
我添加FontAwesomeModule
到应用程序模块
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FontAwesomeModule,
RouterModule,
],
exports: [
FontAwesomeModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(fa: FaIconLibrary, faConfig: FaConfig) {
fa.addIconPacks(fab, fas, far);
faConfig.fixedWidth = true;
}
}
然后ComponentsModule
导入PagesModule
. 我什至尝试同时导入和导出FontAwesomeModule
.
没有什么。
如果您查看模板,我可以使用<fa-icon [icon]="['fab','github']"></fa-icon>
语法。但是在
隐私政策页面,无论如何,我总是得到同样的错误
Error: src/app/pages/privacy-policy/privacy-policy.component.html:3:5 - error NG8001: 'fa-icon' is not a known element:
1. If 'fa-icon' is an Angular component, then verify that it is part of this module.
2. If 'fa-icon' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3 <fa-icon fixedWidth="true" [icon]="warning"></fa-icon>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app/pages/privacy-policy/privacy-policy.component.ts:6:16
6 templateUrl: './privacy-policy.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component PrivacyPolicyComponent.
Error: src/app/pages/privacy-policy/privacy-policy.component.html:3:32 - error NG8002: Can't bind to 'icon' since it isn't a known property of 'fa-icon'.
1. If 'fa-icon' is an Angular component and it has 'icon' input, then verify that it is part of this module.
2. If 'fa-icon' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
3 <fa-icon fixedWidth="true" [icon]="warning"></fa-icon>
~~~~~~~~~~~~~~~~
src/app/pages/privacy-policy/privacy-policy.component.ts:6:16
6 templateUrl: './privacy-policy.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component PrivacyPolicyComponent.
FontAwesomeModule
我根据 FA 说明在应用程序的主模块中导入了。但它仅适用于主页,不适用于子模块中包含的页面。
我怎样才能解决这个问题?
解决方案
如果您有一个托管 PrivacyPolicyComponent 的子模块,那么您需要在该子模块中导入 FontAwesomeModule。
例如,如果您有一个托管 PrivacyPolicyComponent 的“pagesModule”,则在该模块中添加 FontAwesomeModule 而不是 app.module。
推荐阅读
- reactjs - React 和 react-chartjs-2,折线图用字符串(文本)显示数据
- c++ - 如何在函数中更改对象c ++的值
- python - 如何使用pynput收听带有修饰键组合的字母键?
- swift - 在结构的计算属性中访问环境对象属性
- scala - 从父类创建子实例
- python - `libreoffice --headless --convert-to pdf test.docx --outdir /pdf` 在 docker 镜像中不起作用
- python - 如何发送 SYN 数据包而不发送 ACK 响应?
- php - 如何从数组中获取元素?
- laravel - 组件 vuejs / laravel 之间的事件
- javascript - 如何在 jQuery 中使用 iframe 的“execCommand”方法?