我使用的是Angular 2+和Angular CLI。
我如何添加字体awesome到我的项目?
我使用的是Angular 2+和Angular CLI。
我如何添加字体awesome到我的项目?
当前回答
这篇文章描述了如何将Fontawesome 5集成到Angular 6中(Angular 5和以前的版本也可以工作,但你必须调整我的文章)
选项1:添加css-Files
利:每个图标都将包括在内
Contra:每个图标将包括(更大的应用程序大小,因为所有字体都包括在内)
添加以下包:
npm install @fortawesome/fontawesome-free-webfonts
然后在angular.json中添加以下代码行:
"app": {
....
"styles": [
....
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fontawesome.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-regular.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-brands.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-solid.css"
],
...
}
选项2:Angular包
优点:应用程序更小
反:你必须单独包含你想要使用的每个图标
使用FontAwesome 5 Angular包:
npm install @fortawesome/angular-fontawesome
只需按照他们的文档添加图标。他们使用svgs -icons,所以你只需要添加svgs / icons就可以了。
其他回答
这里有很多好的答案。但如果你尝试了所有这些,仍然得到方形而不是方正的图标,检查你的css规则。在我的例子中,我有以下规则:
* {
font-family: Roboto-Light, Roboto, 'Helvetica Neue', sans-serif !important;
}
它覆盖了一些字体。只是将* selector替换为body解决了我的问题:
body {
font-family: Roboto-Light, Roboto, 'Helvetica Neue', sans-serif !important;
}
将它添加到您的包中。json作为 “devDependencies” Font-awesome: "版本号"
进入命令提示符,输入您配置的npm命令。
要在你的Angular项目中使用Font Awesome 5,将下面的代码插入到src/index.html文件中。
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
好运!
这篇文章描述了如何将Fontawesome 5集成到Angular 6中(Angular 5和以前的版本也可以工作,但你必须调整我的文章)
选项1:添加css-Files
利:每个图标都将包括在内
Contra:每个图标将包括(更大的应用程序大小,因为所有字体都包括在内)
添加以下包:
npm install @fortawesome/fontawesome-free-webfonts
然后在angular.json中添加以下代码行:
"app": {
....
"styles": [
....
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fontawesome.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-regular.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-brands.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-solid.css"
],
...
}
选项2:Angular包
优点:应用程序更小
反:你必须单独包含你想要使用的每个图标
使用FontAwesome 5 Angular包:
npm install @fortawesome/angular-fontawesome
只需按照他们的文档添加图标。他们使用svgs -icons,所以你只需要添加svgs / icons就可以了。
在Angular 2.0最终版发布之后,Angular2 CLI项目的结构已经改变了——你不需要任何供应商文件,不需要system.js——只需要webpack。所以你会:
npm install font-awesome --save In the angular-cli.json file locate the styles[] array and add font-awesome references directory here, like below: "apps": [ { "root": "src", "outDir": "dist", .... "styles": [ "styles.css", "../node_modules/bootstrap/dist/css/bootstrap.css", "../node_modules/font-awesome/css/font-awesome.css" // -here webpack will automatically build a link css element out of this!? ], ... } ] ], In more recent versions of Angular, use the angular.json file instead, without the ../. For example, use "node_modules/font-awesome/css/font-awesome.css". Place some font-awesome icons in any html file you want: <i class="fa fa-american-sign-language-interpreting fa-5x" aria-hidden="true"> </i> Stop the application Ctrl + c then re-run the app using ng serve because the watchers are only for the src folder and angular-cli.json is not observed for changes. Enjoy your awesome icons!