【问题标题】:Can't bind to 'icon' since it isn't a known property of 'fa-icon'无法绑定到“图标”,因为它不是“fa-icon”的已知属性
【发布时间】:2018-10-09 02:20:56
【问题描述】:

我在我的项目中使用ngx-share's Share Button Directive。但是,我似乎无法使用这些图标。如果我尝试使用这样的图标:

<button mat-fab shareButton="telegram" [style.backgroundColor]="share.prop.telegram.color">
  <fa-icon [icon]="share.prop.telegram.icon" size="lg"></fa-icon>
</button>

我得到的是以下错误:

Uncaught Error: Template parse errors:
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. ("on mat-fab shareButton="telegram" [style.backgroundColor]="share.prop.telegram.color">
    <fa-icon [ERROR ->][icon]="share.prop.telegram.icon" size="lg"></fa-icon>
  </button>

"): ng:///AppModule/GroupComponent.html@49:13
'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. ("

  <button mat-fab shareButton="telegram" [style.backgroundColor]="share.prop.telegram.color">
    [ERROR ->]<fa-icon [icon]="share.prop.telegram.icon" size="lg"></fa-icon>
  </button>

"): ng:///AppModule/GroupComponent.html@49:4
    at syntaxError (compiler.js:486)
    at TemplateParser.parse (compiler.js:24674)
    at JitCompiler._parseTemplate (compiler.js:34629)
    at JitCompiler._compileTemplate (compiler.js:34604)
    at eval (compiler.js:34505)
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (compiler.js:34505)
    at eval (compiler.js:34375)
    at Object.then (compiler.js:475)
    at JitCompiler._compileModuleAndComponents (compiler.js:34374)

我该如何解决这个问题?


我在导入列表中添加了ShareModule

@NgModule({
  imports: [
    //..
    ShareModule.forRoot()
  ]
})

我的组件还根据需要注入ShareButtons 对象:

export class GroupComponent {    
  constructor(public share: ShareButtons) {
  }
}

【问题讨论】:

  • fa-icon 属于哪个库?需要导入对应的模块
  • @David 我不知道 - 我只是按照网站上的说明进行操作,假设这就是我所要做的。显然,整个事情并没有与编译混淆,而是引入了一些其他问题。安装软件包后,自动重新加载(自动编译)不再起作用。我必须删除这些包才能让它再次工作..
  • 可以分享fa-icon例子的链接吗?
  • @David 当然:Take a look.
  • 现在我得到Template parse errors: 'fa-icon' is not a known element

标签: angular ngx-share


【解决方案1】:

您需要在使用这些分享按钮的模块中导入ShareButtonsModule

import { ShareButtonsModule } from '@ngx-share/buttons';

@NgModule({
  imports: [
    ShareButtonsModule.forRoot()
  ]
})

【讨论】:

  • 这就是我所做的 - 根据安装指南。
  • 顺便说一句,我正在尝试使用指令,而不是组件。
【解决方案2】:

编辑 您还可以导入已经导出FontAwesomeModule ShareButtonModule

原答案

假设您已经安装了 font awesome npm 包,您需要将 FontAwesomeModule 添加到模块的导入中

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';

@NgModule({
 //...
imports: [
     //...
   FontAwesomeModule
  ],
})
export class AppModule { }  

看这里的例子

https://www.npmjs.com/package/@fortawesome/angular-fontawesome

【讨论】:

  • 有趣 - 我安装它的那一刻,自动重新加载中断。如果我对 HTML 进行更改,项目将不再编译,并且网站也不会刷新...
  • 有什么错误吗?在控制台还是在浏览器中?如果您安装了任何东西,请确保您也重新启动 ng serve
  • 这是最好的部分,我认为这就是为什么我之前可能没有注意到共享按钮已经工作的原因,因为我已经尝试安装此模块。我没有收到错误或任何东西。我正在杀死 ng 并重新启动它 ng serve --host 0.0.0.0 - 它不再做任何事情了。
  • 由于这解决了 ngx-share 的问题,我将接受这个答案并打开另一个关于自动重新加载问题的答案。 Take a look 如果您有兴趣。顺便说一句。
  • 所以 ng serve 可以正确编译代码吗?没有错误消息,我无能为力。但是你得到的那个原始错误肯定意味着你错过了 fontawesome 模块
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 2019-11-30
  • 2017-06-21
  • 2017-09-09
  • 2017-02-03
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
相关资源
最近更新 更多