2

我是 Angular 的新手,所以请耐心等待。我正在尝试在我的项目中使用 FontAwesome。最初我通过 CDN 加载它,但我想正确使用它。所以我已经FontAwesomeModuleWidgetModule. 看起来像这样:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { WidgetRoutingModule } from './widget-routing.module';
import { SharedModule } from '@pyb-shared/shared.module';
import { WidgetComponent } from './widget.component';
import { ScenariosComponent } from './scenarios/scenarios.component';
import { QuestionsComponent } from './questions/questions.component';
import { AnswersComponent } from './answers/answers.component';
import { ResultsComponent } from './results/results.component';

import { AnswerButtonComponent } from './shared/answer-button/answer-button.component';
import { HeaderComponent } from './shared/header/header.component';
import { LoadingButtonComponent } from './shared/loading-button/loading-button.component';
import { MainProductComponent } from './shared/main-product/main-product.component';
import { MatchesComponent } from './shared/matches/matches.component';
import { ProductComponent } from './shared/product/product.component';
import { QuestionSplitComponent } from './shared/question-split/question-split.component';

import { AnswersService } from './answers/answers.service';
import { QuestionsService } from './questions/questions.service';
import { ResultsService } from './results/results.service';
import { ScenariosService } from './scenarios/scenarios.service';

import { AnswerMatchService } from './shared/answer-match.service';
import { DuplicateService } from './shared/duplicate.service';
import { FilterService } from './shared/filter.service';
import { FormulaService } from './shared/formula.service';
import { MatchesService } from './shared/matches.service';
import { PickService } from './shared/pick.service';
import { QuestionSplitService } from './shared/question-split/question-split.service';
import { StateService } from './shared/state.service';
import { WidgetService } from './widget.service';

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faSquare, faCheckSquare } from '@fortawesome/pro-solid-svg-icons';

@NgModule({
  imports: [
    CommonModule,
    WidgetRoutingModule,
    SharedModule,
    FontAwesomeModule
  ],
  declarations: [
    WidgetComponent,
    ScenariosComponent,
    QuestionsComponent,
    AnswersComponent,
    ResultsComponent,

    AnswerButtonComponent,
    HeaderComponent,
    LoadingButtonComponent,
    MainProductComponent,
    MatchesComponent,
    ProductComponent,
    QuestionSplitComponent
  ],
  providers: [
    AnswersService,
    QuestionsService,
    ResultsService,
    ScenariosService,

    AnswerMatchService,
    DuplicateService,
    FilterService,
    FormulaService,
    MatchesService,
    PickService,
    QuestionSplitService,
    StateService,
    WidgetService
  ]
})
export class WidgetModule {
  constructor() {
    console.log('WidgetModule loaded.');
    library.add(faSquare, faCheckSquare);
  }
}

我想在我的组件上显示两个图标,因此您可以在上面的模块中看到我添加了faSquarefaCheckSquare。在我的组件(称为ScenariosComponent)中,我只有这个跨度:

<span class="fas fa-square"></span>

从我上面提供的链接,看看这个:

https://github.com/FortAwesome/angular-fontawesome

看起来它应该工作。我导入了FontAwesomeModuleScenariosComponentWidgetModule作为其父模块,所以它应该可以正常工作。根据这条线:

添加到库中的图标将可供其父模块也导入 FontAwesomeModule 的任何其他组件使用。

而且我确实在我的模块中设置了我的库。至少在我的理解中,哪个应该在该模块中工作。

我这样做对吗?

4

1 回答 1

2

如果您想使用angular-fontawesome,则必须遵循他们的指南。您的实施存在一些问题。

第一个问题在这里:

import { faSquare, faCheckSquare } from '@fortawesome/pro-solid-svg-icons';

您正在从中导入@fortawesome/pro-solid-svg-iconsFortAwesome 的专业版。

您应该从以下位置导入@fortawesome/free-solid-svg-icons

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faSquare, faCheckSquare } from '@fortawesome/free-solid-svg-icons';

然后在您的 AppModule 中:

export class AppModule {
  constructor() {
    library.add(faSquare, faCheckSquare);
  }
}

第二个问题是,从 Guide forangular-fontawesome看来,您必须将其用作 Component:fa-icon它有一个@Input属性[icon],您必须提供要应用到它们的类的名称.

像这样的东西:

<div style="text-align:center">
  <fa-icon [icon]="['fas', 'square']"></fa-icon>
  <br>
  <fa-icon [icon]="['fas', 'check-square']"></fa-icon>
</div>

这是您参考的示例 StackBlitz

于 2018-11-23T16:58:13.583 回答