简体   繁体   English

Angular 8 中的 FontAwesome,最好的导入方式?

[英]FontAwesome in Angular 8, best way to import?

I'm using fontawesome 5.x in my angular 8+ app, and the system to import the icons is very very awfull to me.我在我的 angular 8+ 应用程序中使用 fontawesome 5.x,导入图标的系统对我来说非常非常糟糕。 This is how I do: (like documentation say https://github.com/FortAwesome/angular-fontawesome )我就是这样做的:(就像文档说https://github.com/FortAwesome/angular-fontawesome

import { Component, OnInit } from '@angular/core';
import {faHome,faSearch,faArchive,faFileSignature,
    faAddressBook,faUserMd,faDesktop} from '@fortawesome/free-solid-svg-icons/'

@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {
faHome = faHome;
faSearch = faSearch;
faArchive = faArchive;
faFileSignature = faFileSignature;
faAddressBook = faAddressBook;
faUserMd = faUserMd;
faDesktop = faDesktop;
constructor() { }

ngOnInit() {
}

}

This can be okay if you have < 10 icons, but if you have +10 I think this is awfull.如果你有 < 10 个图标,这可能没问题,但如果你有 +10,我认为这很糟糕。 Is there another better way to import this?还有其他更好的方法来导入它吗? I was searching the css file of fontawesome to "import it" at scripts of angular.json like I do with bootstrap popper.js and jquery but didnt found it in node_modules. I was searching the css file of fontawesome to "import it" at scripts of angular.json like I do with bootstrap popper.js and jquery but didnt found it in node_modules.

So, is there another another way to do this more "clean"?那么,还有另一种方法可以做到这一点更“干净”吗? (not CDN way) (不是CDN方式)

[Resolved] [解决]

Okay I was importing the fontawesome library for desktop apps, no for web apps.好的,我正在为桌面应用程序导入 fontawesome 库,对于 web 应用程序没有。 With web apps package I'm okay doing like I do.使用 web 应用程序 package 我可以像我一样做。

Thank you all.谢谢你们。

How I import fontAwesome icons in Angular8+我如何在 Angular8+ 中导入 fontAwesome 图标

1º Step npm install --save-dev @fortawesome/fontawesome-free 1º 步骤npm install --save-dev @fortawesome/fontawesome-free

2º Step in angular.json paste the route to css and js of @fortawesome:angular.json中的2º 步骤将路径粘贴到 css 和 @fortawesome 的 js 中:

  "styles": [
          "src/styles.css",
          "node_modules/bootstrap/dist/css/bootstrap.min.css",
          "node_modules/@fortawesome/fontawesome-free/css/all.min.css" // this
        ],
        "scripts": [ 
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/popper.js/dist/umd/popper.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.min.js",
          "node_modules/@fortawesome/fontawesome-free/js/all.min.js" //this
        ]

This was what I wanted.这就是我想要的。 With this you only need to paste the icon (for example: <i class="fas fa-home"></i> ) in html.这样您只需将图标(例如: <i class="fas fa-home"></i> )粘贴到 html 中。

The other way I post was for desktop apps I think.我发布的另一种方式是我认为的桌面应用程序。

have you try this?你试过这个吗?

import { Component, OnInit } from '@angular/core';
import * as icons from '@fortawesome/free-solid-svg-icons/'

@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {

  constructor() {
  }

  ngOnInit() {   
  }

}

This is quick solution but not a good one.这是一个快速的解决方案,但不是一个好的解决方案。

Best solution is depend on your project structure.最佳解决方案取决于您的项目结构。

Case 1. If you have a small project with App Module only then you can create a different fontawesome-icon.module (like we do in the angular material) and import as well export the required icons in that and finally import this module in the app-routing.module (if you are using routing module else import this in app.module).案例 1. 如果你有一个只有 App Module 的小项目,那么你可以创建一个不同的 fontawesome-icon.module(就像我们在 angular 材料中所做的那样)并导入以及导出所需的图标,最后在app-routing.module(如果您使用的是路由模块,请在 app.module 中导入此模块)。

Case 2. If you are working with multiple feature modules then the best way is to create shared.module and import fontawesome-icon.module in the shared.module and then import this shared.module further to the various respective feature routing.module.案例 2. 如果您正在使用多个功能模块,那么最好的方法是创建 shared.module 并在 shared.module 中导入 fontawesome-icon.module,然后将此 shared.module 进一步导入各个相应的功能 routing.module。

Also check for object and array de-structuring if you are dealing with lots of values.如果您要处理大量值,还要检查 object 和数组解构。

I hope this will help我希望这个能帮上忙

Documentation shows the best way to do that itself.文档显示了做到这一点的最佳方式。 Why we have to include all the modules separately is because in angular we are only allowed to expose that module which we are using.为什么我们必须分别包含所有模块是因为在 angular 中,我们只允许公开我们正在使用的模块。 Because this will surely affect the performance.因为这肯定会影响性能。

Eg You have 1000 Icons in the Fontawesome and you are just using 100 of it then is it worth so to import all the 1000 Icons?例如,您在 Fontawesome 中有 1000 个图标,而您只使用其中的 100 个,那么导入所有 1000 个图标是否值得?

I hope this clears your understanding about the exports.我希望这能清除您对出口的理解。

Still, if you want you can create a different module for fontawesome and import it to your main module.不过,如果您愿意,您可以为 fontawesome 创建一个不同的模块并将其导入您的主模块。 So that your main modules look neat and clean.让你的主要模块看起来整洁干净。

Thanks..: :)谢谢..: :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM