在角形材料7

时间:2018-10-27 18:43:00

标签: font-awesome-5 icon-fonts angular7 angular-material-7

我使用的是角7.0.1和角材质7.0.2,我想添加真棒字体5.4.2图标,并且尝试按照fontawesome网站上的步骤操作,但是我无法获得真棒字体图标字体。

第一:

npm install --save-dev @fortawesome/fontawesome-free

然后在styles.scss中添加:

@import '~@fortawesome/fontawesome-free/scss/fontawesome.scss';

并在_variables.scss中添加:

$fa-font-path: '~@fortawesome/fontawesome-free/webfonts';

现在在app.component.ts中添加:

import { MatIconRegistry } from "@angular/material";

[...]

constructor(
    public matIconRegistry: MatIconRegistry,
  ) {
    matIconRegistry.registerFontClassAlias ('fas');  // tried with 'fas' and with 'fa'
  }

最后我不得不用以下命令打印字体很棒的图标:

<mat-icon mat-list-icon fontIcon="fas github"></mat-icon>  // tryed also with 'fas-github', 'fasGithub', 'github', 'fa-github', 'fa github' and 'faGithub'

但是从未打印过字体超赞的图标字体。我错过了一些重要且显而易见的东西,但是现在我没有看到它。

7 个答案:

答案 0 :(得分:8)

这是我所做的,对我有用:

在我的styles.scss中,

@import "~@fortawesome/fontawesome-free/css/all.css";

那我有

<mat-icon fontSet="fa" fontIcon="fa-clipboard-check"></mat-icon>

使用scss对我也不起作用。

答案 1 :(得分:4)

如果您使用scss,则只需将样式导入到vendor.scss:

$fa-font-path: "~@fontawesome/fontawesome-free/webfonts";
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
@import "~@fortawesome/fontawesome-free/scss/regular";
@import "~@fortawesome/fontawesome-free/scss/solid";
@import "~@fortawesome/fontawesome-free/scss/brands";

并正常使用它:

<span class="fab fa-refresh fa-spin fa-github" aria-hidden="true"></spin>

注意名称空间, fab 用于品牌, fas 用于实体等。

那对我来说是最好的选择。


或者您可以使用angular-fontawesome库。

答案 2 :(得分:1)

我遵循以下简单步骤, 它可以在我的 Angular 7 项目中安装真棒5.6.3 免费版本)。

运行以下命令以安装真棒字体的最新版本。

npm install --save-dev @fortawesome/fontawesome-free

在angular.json文件中添加文件路径。

"styles": ["node_modules/@fortawesome/fontawesome-free/css/all.css"],
"scripts": ["node_modules/@fortawesome/fontawesome-free/js/all.js"]

以供参考:https://fontawesome.com/how-to-use/on-the-web/setup/using-package-managers

全力以赴。 希望这会有所帮助..

答案 3 :(得分:1)

我使用这两种解决方案:

大量使用的图标

导入主fontawesome.scss文件。

导入首选样式文件,例如regular.scss(在angular.json中或通过@import)。

在模块中注册默认字体集,因此您不必为每个图标定义它:

constructor(private matIconRegistry: MatIconRegistry) {
    matIconRegistry.setDefaultFontSetClass('far'); // or 'fas' for solid style etc.
}

最后定义图标行:

<mat-icon fontIcon="fa-smile"></mat-icon>

使用的图标很少

不需要导入所有图标,但是您只需要在模块中分别定义每个图标。另一个优点是,您可以定义自己的图标名称,以提高清晰度,可以将具有不同样式的图标(纯色,十二指肠...)组合在一起,而无需加载其他图标集,或者只需添加自己的svg图标或徽标即可。

constructor(private matIconRegistry: MatIconRegistry,
            private sanitizer: DomSanitizer) {
    matIconRegistry.addSvgIcon('smile', sanitizer.bypassSecurityTrustResourceUrl('smile.svg'));
}

图标:

<mat-icon svgIcon="smile"></mat-icon>

答案 4 :(得分:0)

    <fa-icon icon="fa-clipboard-check"></fa-icon>

确保将FontAwesomeModule导入到组件的容器模块中。

如果您需要svg:

    import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
    import { library } from '@fortawesome/fontawesome-svg-core';
    import {faClipboardCheck} from '@fortawesome/free-solid-svg-icons';
    library.add(faClipboardCheck);

答案 5 :(得分:0)

我下载了真棒字体包,并将/ webfonts文件夹和/css/all.css文件复制到src / assets /。我在angular.json的构建选项中的样式数组中添加了src / assets / css / all.css路径,如下所示。

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "NAME_OF_PROJECT": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/NAME_OF_PROJECT",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/.well-known"
            ],
            "styles": [
            "src/styles.scss",
            "src/assets/css/all.css",
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        }
      }
    }}
}

现在,字体真棒图标可在您的角度应用程序中使用。我使用fa-2x类来控制图标的大小。将fontSet更改为“ fal”将为您提供超酷的字体版本。

<mat-icon fontSet="fa" class="fa-2x" fontIcon="fa-user" matBadge="8" matBadgeSize="medium" matBadgePosition="after" matBadgeColor="warn"></mat-icon>

答案 6 :(得分:0)

如果您想在<mat-icon>内使用“真棒字体”图标,请像下面这样使用

<mat-icon><i class="fa fa-file-chart-line"></i></mat-icon>

**您应该已经将Font Awesome CSS或SCSS导入到项目中