问题
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. So this poses the question, how do you add and use Font Awesome in an Ionic 4 project?
I have tried using the following tutorial without much success. I tried following the steps outlined in the following StackOverflow answer which did not work either.
回答1:
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:
<fa-icon [icon]="['fas', 'graduation-cap']" slot="end"></fa-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
.
回答2:
Just in case if someone deals with FontAwesome PRO. I've recently bought FontAwesome pro icons and integreted them like this:
- copy the FontAwesome webfonts folder in src/assets/
- copy the FontAwesome scss folder in src/theme/
change the $fa-font-path in _variables.scss with
assets/webfonts !default;
add in global.scss
@import './theme/[YourDirectoryfontawesomeScss]/fontawesome.scss';
@import './theme/[YourDirectoryfontawesomeScss]/solid.scss';
@import './theme/[YourDirectoryfontawesomeScss]/brands.scss';
@import './theme/[YourDirectoryfontawesomeScss]/light.scss';
@import './theme/[YourDirectoryfontawesomeScss]/regular.scss';
Finally you can use them with the i tag. For example
<i class="fas fa-stack-overflow"></i>
you can find more details about the fa- classes here https://fontawesome.com/how-to-use/on-the-web/setup/getting-started
回答3:
Run the following command:
npm install --save-dev @fortawesome/fontawesome-free
Then, in angular.json
append this on "styles":
{
"input": "node_modules/@fortawesome/fontawesome-free/css/all.css"
}
来源:https://stackoverflow.com/questions/54324300/how-do-you-add-font-awesome-to-ionic-4