【问题标题】:Angular 5 with Font Awesome 5Angular 5 和 Font Awesome 5
【发布时间】:2018-07-23 07:56:45
【问题描述】:

经过一些研究,包括阅读Font Awesome 5 with Angular、documentation等,我无法推进Angular 5 App和Font-Awesome 5 (Pro)的集成。

问题:图标不出现。

关于我当前场景的更多详细信息:

package.json:

(...)
"dependencies": {
"@angular/animations": "^5.2.4",
"@angular/common": "^5.2.4",
"@angular/compiler": "^5.2.4",
"@angular/core": "^5.2.4",
"@angular/forms": "^5.2.4",
"@angular/http": "^5.2.4",
"@angular/platform-browser": "^5.2.4",
"@angular/platform-browser-dynamic": "^5.2.4",
"@angular/router": "^5.2.4",
"@fortawesome/fontawesome": "^1.1.3",
"@fortawesome/fontawesome-pro-light": "^5.0.6",
"bootstrap": "^4.0.0",
"classlist.js": "^1.1.20150312",
"core-js": "^2.5.3",
"jquery": "^3.3.1",
"jquery-ui": "^1.12.1",
"popper.js": "^1.12.9",
"prismjs": "^1.11.0",
"rxjs": "^5.5.6",
"summernote": "^0.8.9",
"sweetalert2": "^7.11.0",
"web-animations-js": "^2.3.1",
"zone.js": "^0.8.20"
},
"devDependencies": {
"@angular/cli": "^1.7.0-rc.0",
"@angular/compiler-cli": "^5.2.4",
"@angular/language-service": "^5.2.4",
"@types/jasmine": "^2.8.6",
"@types/jasminewd2": "^2.0.3",
"@types/node": "^9.4.5",
"codelyzer": "^4.1.0",
"jasmine-core": "^2.99.1",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^2.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "^1.0.1",
"karma-coverage-istanbul-reporter": "^1.4.1",
"karma-jasmine": "^1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "^5.3.0",
"ts-node": "^4.1.0",
"tslint": "^5.9.1",
"typescript": "^2.7.1"
}
(...)

angular-cli.json

    (...)
    "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/popper.js/dist/umd/popper.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js",
    "../node_modules/jquery-ui/ui/data.js",
    "../node_modules/jquery-ui/ui/disable-selection.js",
    "../node_modules/jquery-ui/ui/scroll-parent.js",
    "../node_modules/jquery-ui/ui/widget.js",
    "../node_modules/jquery-ui/ui/widgets/mouse.js",
    "../node_modules/jquery-ui/ui/widgets/sortable.js",
    "../node_modules/sweetalert2/dist/sweetalert2.min.js",
    "../node_modules/summernote/dist/summernote.min.js",
    "../node_modules/@fortawesome/fontawesome/index.js (tried with and without this file)"
  ], (...)

在我的研究过程中,我了解到我需要指定我需要使用的(图标),如下所示:

import {faChevronLeft, faChevronRight} from '@fortawesome/fontawesome-free-solid';
import fontawesome from '@fortawesome/fontawesome';

然后我需要将这些图标传递给 fontawesome 对象:

fontawesome.library.add(faChevronLeft, faChevronRight);

最后我发现(在font-awesome documentation 中,我需要调用负责使魔法发生的 i2svg 方法:

fontawesome.dom.i2svg();

文档说我必须调用不带参数的方法。但是编译器将我指向另一个方向,我必须告知以下数据:

export interface DOM {
  i2svg(params: { node: Node; callback: () => void }): void;
  css(): string;
  insertCss(): string;
}

到目前为止,我达到的最大值是由 font-awesome 制作的浏览器中的日志,在此代码之后带有以下信息“fontawesome.dom.i2svg()”:

fontawesome.dom.i2svg({ node: document.body, callback: callbacki2Svr });

function callbacki2Svr() { }

我已经尝试了很多(null 导致“Uncaught TypeError: Cannot read property 'querySelectorAll' of null”),现在我无法前进。有人有什么建议吗?

安装:

1) 按照use node js 上的说明进行操作([TOKEN] 是我从购买中收到的密钥):

 npm i --save @fortawesome/fontawesome
 npm config set @fortawesome:registry https://npm.fontawesome.com/[TOKEN]

 Created a .npmrc file in the root of my project with the following content:
 @fortawesome:registry=https://npm.fontawesome.com/[TOKEN]
 npm i --save @fortawesome/fontawesome-pro-light

2) 更新了 angular-cli 3) 使用 (ng b -aot) 重建应用程序 4) 启动项目。

提前谢谢你!

示例 HTML 代码 登录页面(无“i”标签)

<div class="login-page">
  <div class="wrapper-login">
    <img src="assets/image/logo/horizontal-bew.svg" />
    <raptor-textbox #txtUser type="text" iconClass="fa fa-user-o" iconPosition="right" placeholder="Login"
                [(value)]="userName" (onKeyUp)="updateUserName($event)" (onEnterPressed)="userKeyEnter()"></raptor-textbox>
    <raptor-textbox #txtPwd type="password" iconClass="fa fa-key" iconPosition="right" placeholder="Senha"
                [value]="pwd" (onKeyUp)="updatePwd($event)" (onEnterPressed)="pwdKeyEnter()"></raptor-textbox>
     <raptor-button cssClass="btn btn-light btn-block" text="Entrar" (click)="authenticate()"></raptor-button>
     <a [attr.href]="'#loginModal'" data-toggle="modal" [attr.data-target]="'#loginModal'">Forgot Password</a>
  </div>
</div>

<raptor-modal title="Forgot Password"
          icon="fa fa-envelope-o"
          cssClass="fade"
          classModal="modal-dialog modal-dialog-centered"
          modalId="loginModal"
          (onSavedClicked)="saveForm($event)"
          (onCancelClicked)="cancelModal($event)">

  <div class="row">
    <div class="form-group col-12">
      <label>Email</label>
      <raptor-textbox type="email" typeValue="email" applyValidation="true" applyMask="true" icon="fa fa-envelope" iconPosition="right" placeholder="E-mail..."></raptor-textbox>
    </div>
  </div>
</raptor-modal>

Raptor 按钮(带有“i”标签)

<button type="button" [class]="cssClass" (click)="click()" [attr.data-target]="dataTarget" [attr.disabled]="isDisabled ?'' : null">
  <i *ngIf="hasIcon()" class="{{ icon }}"></i><span>{{ text }}</span>
</button>

/**
* verify if icon was defined
* @hasIcon
*
*/
public hasIcon() {
  return this.icon && this.icon.length > 0;
}

【问题讨论】:

  • 可以分享一下相关的html模板或者组件代码吗?似乎在 fontawesome.dom.i2svg({ node: document.body, callback: callbacki2Svr }) 中用于查找 标记的“document.body”为空。这就是为什么它会抛出“Uncaught TypeError: Cannot read property 'querySelectorAll' of null”
  • 尝试添加 angular-font-awesome。配置在这里:npmjs.com/package/angular-font-awesome
  • @Janarthanan,按照您的建议进行编码。只有一点:当我传递 null 时会发生异常。传递 document.body 只会在控制台中打印一条消息(“fontawesome.dom.i2svg()”)。
  • @SaptarshiBasu,谢谢你的建议。这个包使用font-awesome 4.7版本,但我打算使用我购买的那个:5.0。
  • 您在哪里指定您使用的图标?我的意思是,有人告诉我我需要像您在 global 区域中那样将图标添加到库字段,但我不确定 global 区域的含义在项目中。

标签: angular angular-cli angular5 font-awesome-5 angular-fontawesome


【解决方案1】:

我们正在开发一个官方的 Angular 组件(目前处于预发布阶段)https://github.com/FortAwesome/angular-fontawesome

但是,@daniel-santana 告诉我们,这不可能集成到他们的大型应用程序中。

所以我能给你的最好建议是不要使用带有 JS 的 SVG 方法。它使用了一些高级功能,例如 requestAnimationFrame 和 MutationObservers,这些功能在 Angular 中表现不佳。如果您无法重构您的应用程序以使用新的 SVG 格式(这是 Font Awesome 5 中的新格式),请坚持使用Web Fonts。版本 5 的 Web 字体与版本 4 中的 Web 字体一样工作。升级到 5 将涉及更改已重命名的任何图标的名称。我们有详细的docs here。

【讨论】:

  • 谢谢你,Rob 会做的。我把你的问题作为解决问题的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 2020-02-10
  • 1970-01-01
  • 2023-01-29
  • 2019-04-17
  • 2019-02-26
相关资源
最近更新 更多