How do you add Font Awesome to Ionic 4

前端 未结 5 657
梦如初夏
梦如初夏 2021-01-02 05:11

There are a lot of tutorials and articles of how to include Font Awesome in an Ionic 3 project but I struggled finding any on how to add Font Awesome into an Ionic 4 project

5条回答
  •  Happy的楠姐
    2021-01-02 05:58

    To get Font Awesome working in an Ionic 4 project you can follow the steps below.

    Firstly, you need to install all the npm packages, the first two are required but you can decide whether you need the solid, regular or brands icons, I will be using all of them. Go ahead and execute the following npm commands in your terminal:

    npm i --save @fortawesome/fontawesome-svg-core
    npm i --save @fortawesome/angular-fontawesome
    npm i --save @fortawesome/free-solid-svg-icons
    npm i --save @fortawesome/free-regular-svg-icons
    npm i --save @fortawesome/free-brands-svg-icons
    

    Once you have done that, navigate to your app.module.ts in your project and add the following:

    import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
    import { library } from '@fortawesome/fontawesome-svg-core';
    

    Depending on which font packages you installed, add the following:

    import { fas } from '@fortawesome/free-solid-svg-icons';
    import { far } from '@fortawesome/free-regular-svg-icons';
    import { fab } from '@fortawesome/free-brands-svg-icons';
    

    Once they have been imported at the top of your file you will need to include the FontAwesomeModule in your imports:

    .....
    imports: [...., FontAwesomeModule],
    .....
    

    Once again, depending on what icons you chose you will need to add them to the Font Awesome library that was imported earlier. Add this underneath your last import (above @NgModule()):

    library.add(fas, far, fab);
    

    Then navigate to the module of the page that you would like to use the fonts in i.e. home.module.ts and then import and add the FontAwesomeModule:

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

    Then finally you can use the icon in that page's HTML by inserting the following where you'd like the icon:

    
    

    You can replace, fas with the correct library i.e. far, fas & fab and then the name of the icon, which in this case was graduation-cap.

提交回复
热议问题