javascript - 添加 Font Awesome 图标

标签 javascript angular font-awesome

我想添加 Font Awesome 图标以在我按照 this MOOC 开发的应用程序中使用但它产生了一个我不明白的错误。

首先,我使用 NPM 在项目中搜索 Font Awesome,在提示符处键入以下内容:

npm install font-awesome@4.7.0 --save

然后,我在 src 文件夹中添加了一个名为 _variables.scss 的新文件,并添加以下内容:

$fa-font-path : '../node_modules/font-awesome/fonts';

然后,打开styles.scss文件,更新如下:

. . .

@import 'variables';
@import '../node_modules/font-awesome/scss/font-awesome';

. . .

但是当我使用 ng serve --open 启动服务时,我得到以下信息:

C:\Users\antoi\Documents\Programming\Teaching\Me\FullStack\Angular\conFusion>ng serve --open 10% building 3/3 modules 0 activei“wds”:项目正在运行http://localhost:4200/webpack-dev-server/ i “wds”:webpack 输出来自/ i 「wds」: 404s 将退回到//index.html

chunk {main} main.js, main.js.map (main) 2.08 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 122 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.09 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 394 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 339 kB [initial] [rendered]
Date: 2019-08-26T10:11:22.403Z - Hash: 8fbb15623a903204f6d1 - Time: 11587ms

ERROR in node_modules/@angular/flex-layout/core/typings/match-media/mock/mock-match-media.d.ts:62:22 - error TS2420: Class 'MockMediaQueryList' incorrectly implements interface 'MediaQueryList'.
  Type 'MockMediaQueryList' is missing the following properties from type 'MediaQueryList': onchange, addEventListener, removeEventListener, dispatchEvent

62 export declare class MockMediaQueryList implements MediaQueryList {
                        ~~~~~~~~~~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/mock/mock-match-media.d.ts:79:27 - error TS2304: Cannot find name 'MediaQueryListListener'.

79     addListener(listener: MediaQueryListListener): void;
                             ~~~~~~~~~~~~~~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/mock/mock-match-media.d.ts:81:23 - error TS2304: Cannot find name 'MediaQueryListListener'.

81     removeListener(_: MediaQueryListListener): void;
                         ~~~~~~~~~~~~~~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/server-match-media.d.ts:11:22 - error TS2420: Class 'ServerMediaQueryList' incorrectly implements interface 'MediaQueryList'.
  Type 'ServerMediaQueryList' is missing the following properties from type 'MediaQueryList': onchange, addEventListener, removeEventListener, dispatchEvent

11 export declare class ServerMediaQueryList implements MediaQueryList {
                        ~~~~~~~~~~~~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/server-match-media.d.ts:28:27 - error TS2304: Cannot find name 'MediaQueryListListener'.

28     addListener(listener: MediaQueryListListener): void;
                             ~~~~~~~~~~~~~~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/server-match-media.d.ts:30:23 - error TS2304: Cannot find name 'MediaQueryListListener'.

30     removeListener(_: MediaQueryListListener): void;
                         ~~~~~~~~~~~~~~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/server-match-media.d.ts:42:15 - error TS2416: Property '_registry' in type 'ServerMatchMedia' is not assignable to the same property in base type 'MatchMedia'.
  Type 'Map<string, ServerMediaQueryList>' is not assignable to type 'Map<string, MediaQueryList>'.
    Type 'ServerMediaQueryList' is missing the following properties from type 'MediaQueryList': onchange, addEventListener, removeEventListener, dispatchEvent

42     protected _registry: Map<string, ServerMediaQueryList>;
                 ~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/match-media/server-match-media.d.ts:54:15 - error TS2416: Property '_buildMQL' in type 'ServerMatchMedia' is not assignable to the same property in base type 'MatchMedia'.
  Type '(query: string) => ServerMediaQueryList' is not assignable to type '(query: string) => MediaQueryList'.
    Type 'ServerMediaQueryList' is not assignable to type 'MediaQueryList'.

54     protected _buildMQL(query: string): ServerMediaQueryList;
                 ~~~~~~~~~
node_modules/@angular/flex-layout/core/typings/observable-media/observable-media.d.ts:11:14 - error TS2416: Property 'subscribe' in type 'ObservableMedia' is not assignable to the same property in base type 'Subscribable<MediaChange>'.
  Type '(next?: (value: MediaChange) => void, error?: (error: any) => void, complete?: () => void) => Subscription' is not assignable to type '{ (observer?: PartialObserver<MediaChange>): Unsubscribable; (next: null, error: null, complete: () => void): Unsubscribable; (next: null, error: (error: any) => void, complete?: () => void): Unsubscribable; (next: (value: MediaChange) => void, error: null, complete: () => void): Unsubscribable; (next?: (value: Medi...'.
    Types of parameters 'next' and 'observer' are incompatible.
      Type 'PartialObserver<MediaChange>' is not assignable to type '(value: MediaChange) => void'.
        Type 'NextObserver<MediaChange>' is not assignable to type '(value: MediaChange) => void'.
          Type 'NextObserver<MediaChange>' provides no match for the signature '(value: MediaChange): void'.

11     abstract subscribe(next?: (value: MediaChange) => void, error?: (error: any) => void, complete?: () => void): Subscription;
                ~~~~~~~~~

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
i 「wdm」: Failed to compile.

代码在 MOOC 中,但如果您需要存储库,请告诉我。

实现

我执行了 ng update --all 并且不再有错误。但是在 app.component.html 添加组件页脚和页眉时

<app-header></app-header>
<app-menu></app-menu>
<app-footer></app-footer>

我在本地主机上没有更多渲染。但是如果我删除了页眉和页脚,我又得到了渲染。但是,对于第一种情况和另一种情况,我没有错误。

最佳答案

你可以试试angular-font-awesome

npm i angular-font-awesome --save

现在在 app.module.ts 中添加模块:

import {AngularFontAwesomeModule} from 'angular-font-awesome';

然后在您的导入数组中也添加它

imports: [
    ..
    AngularFontAwesomeModule
];

关于javascript - 添加 Font Awesome 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57664061/

相关文章:

html - FontAwesome 5 - 多色图标

laravel - 尝试让删除按钮显示图标

Javascript 在函数执行后显示更多/更少问题

angular - 命令 "npm run build -- --prod"退出并出现代码 1 错误

Angular 9 mat-progress-buttons 错误 : ERROR in Failed to compile entry-point mat-progress-buttons (es2015 as esm2015) due to compilation errors

css - 设置 Font Awesome 图标位置

javascript - 如何根据其内容的高度调整 iframe 的高度?

javascript - 返回函数的值而不是 Array.forEach 中的内部函数

javascript - 使用 jQuery 更高效地根据条件查找每个表行中的第 n 个单元格?

javascript - Angular2如何做一个url查询字符串