简体   繁体   中英

How to use fontawesome icons in angular 13

Itried using font awesome icons But I can't use it. I imported it in angular.json also but i cn't use it why?

In order to use fontawesome icons, you need to install the following 3 packages.

"@fortawesome/angular-fontawesome": "^0.7.0",
"@fortawesome/fontawesome-svg-core": "^1.2.28",
"@fortawesome/free-solid-svg-icons": "^5.13.0",

And in the module file, you need to import FontAwesomeModule

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    FontAwesomeModule,
    ...
  ],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

And in the component file, you need to import specific font icon component.

import { faHeart, faComment } from '@fortawesome/free-solid-svg-icons';

export class YourComponent implements OnInit {
    faHeart = faHeart;
    faComment = faComment;
}

Lastly, in the html file you can use this icons.

<fa-icon [icon]="faHeart"></fa-icon>

Hope it could help you.

I just import font awesome in angular.json file like this:

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

Then I can use regular font awesome syntax in html, i.ex.

<i class="fa-solid fa-bus"></i>

There is 1 problem with that solution - whole font-awesome's code is paste to main style.css which increase file size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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