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.
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.
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"
}
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
I was racking my brain trying to get this working with Ionic 5/Angular 10 and couldn't get it working. I figured it in the end, should anyone else require it.
For Ionic 5/Angular 10
Run ng add #fortawesome/angular-fontawesome#latest in your project folder and select the icons you require.
In app.module.ts add the following:
import { FaIconLibrary, FontAwesomeModule } from '#fortawesome/angular-fontawesome';
import { fas } from '#fortawesome/free-solid-svg-icons'; //Solid icons
import { far } from '#fortawesome/free-regular-svg-icons'; // Regular icons
import { fab } from '#fortawesome/free-brands-svg-icons'; // Brand icons
And import FontAwesomeModule to the imports declarables so it looks like this:
#NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, FontAwesomeModule],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent],
})
And then export a library constructor as so...
export class AppModule {
constructor(library: FaIconLibrary) {
library.addIconPacks(fas, far, fab);
}
}
Go to the module you wish to use Font Awesome (e.g. mypage.module.ts) and import the FontAwesomeModule.
Finally in your HTML, e.g. mypage.page.html simply use <fa-icon icon="home"></fa-icon> to display an icon.
If you run in to any issues, make sure you stop Ionic first and run ionic serve again as this should recompile the framework.
Also take a look at https://github.com/FortAwesome/angular-fontawesome/blob/master/docs/usage/features.md for a full list of the features available in its usage.
The easiest way for the Angular user is to write ng add #fortawesome/angular-fontawesome#latest
Related
I am attempting to use the ion-bottom-drawer component in my app but i keep running into errors. The component does not seem to be recognized
Steps to reproduce :
create ionic4 project
npm i ion-bottom-drawer --save
npm i hammerjs#2.0.8 --save
npm i #types/hammerjs#2.0.36 --save
import IonBottomDrawerModule in app.module.ts
add component to homepage
<ion-bottom-drawer
[(state)]="drawerState"
[minimumHeight]="minimumHeight"
[dockedHeight]="dockedHeight"
[shouldBounce]="shouldBounce"
[distanceTop]="distanceTop"
>
<div class="drawer-content">
Bottom Drawer Content
</div>
</ion-bottom-drawer>
The component is not recognized despite following all steps in the documentation
You should use the appropriate values.
Please use as below.
<ion-bottom-drawer [minimumHeight]=50 [dockedHeight]=350
[shouldBounce]=true [distanceTop]=50>
<div class="drawer-content">
Bottom Drawer Content
</div>
</ion-bottom-drawer>
Here is an example
https://stackblitz.com/edit/ionic-bottom-drawer?file=pages%2Fhome%2Fhome.ts
Look in your homepage.module.ts and import it in there as well:
#NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild([
{
path: '',
component: HomePage
}
]),
IonBottomDrawerModule
],
declarations: [HomePage]
})
export class HomePageModule {}
I'm still learning myself, but I think this is because of the lazy loading feature. Every page gets its own module.
I finally figured it out. I had to create a component module and import IonBottomDrawerModule there then include CUSTOM_ELEMENTS_SCHEMA as a schema in the module and just use the component the way the documentation states.
I need to run the code “this.backgroundMode.enable()” in my project, but it shows me the following error:
"Object(...) is not a function"
It imports it in app.module.ts in the following way:
import {BackgroundMode} from '# ionic-native / background-mode / ngx';
...
providers: [
...
BackgroundMode
...]
And in the page (in my case is in app.component.ts, after deviceready, like the official documentation says) i use like:
import {BackgroundMode} from '# ionic-native / background-mode / ngx';
constructor(private backgroundMode: BackgroundMode) { }
...
this.backgroundMode.enable();
Please I need to run this plugin in my project
I have answered a similar question here https://stackoverflow.com/a/54398403/6617276
Check your project type in ionic.config.json file.
If the type is "ionic-angular", then install 4.x.x version.
npm i -s #ionic-native/background-mode#4.20.0
If the type is "angular", then install 5.x.x-beta version
npm i -s #ionic-native/background-mode#5.0.0-beta.24
Note:
Add ngx at the end of import only if you are using Angular 6
import { BackgroundMode } from '#ionic-native/background-mode/ngx';
if not remove ngx from the import both in app.module.ts and app.component.ts
import { BackgroundMode } from '#ionic-native/background-mode';
I'm trying to add OfficeUI fabric components in a blog build using gatsby js.
As soon as I'm importing any component, the site stop to works.
Using develop command, I can see in the browser console : SyntaxError: export declarations may only appear at top level of a module
How to fix this ? (I'm very new to node dev).
Searches I've done suggest problems with babel not using the es2015 preset. However, I double checked, the .babelrc file is mentioning this preset.
Here's the complete operations I've done (on Windows 10 x64 if it matters):
cloned the gatsby-starter-blog-no-styles repo :
gatsby.cmd new someblog https://github.com/noahg/gatsby-starter-blog-no-styles
cd someblog
npm install
drink a coffee (will move to yarn soon)
Check that works
gatsby develop
Opened the browser (http://localhost:8000). Its Ok
added office ui fabric react components
npm install --save office-ui-fabric-react
Restart gatsby develop. Still working
change src/layouts/index.js file to import an office component
import React from 'react'
import Link from 'gatsby-link'
import { Button } from 'office-ui-fabric-react/lib/Button'
class Template extends React.Component {
....
And voilà! it stop to works. In the browser console, I see an error : SyntaxError: export declarations may only appear at top level of a module
I put in GH a complete reproduction repository : https://github.com/stevebeauge/repro-gatsbyjs-officeui-error
[Edit] Digging a bit I can see in the generated 'common.js' file the error :
/***/ "./node_modules/office-ui-fabric-react/lib/Button.js":
/***/ (function(module, exports) {
export * from './components/Button/index';
//# sourceMappingURL=Button.js.map
/***/ }),
The export here seems to be forbidden, which leads to Babel issue (not found how to solve though)
Recently i stumbled upon the similar error, my solution was to explicitly import from lib-commonjs:
import { Button } from 'office-ui-fabric-react/lib-commonjs/Button';
instead of
import { Button } from 'office-ui-fabric-react/lib/Button'
Seems to be the error occurs since babel isn't converting office-ui-fabric-react to CommonJS module.
I am using create-react-app and trying to install mapbox-gl-draw.
npm install #mapbox/mapbox-gl-draw
This works with some npm warnings. I then try to pull mapbox-gl-draw into a component like this:
import React,{Component} from 'react';
import mapboxgl from 'mapbox-gl/dist/mapbox-gl.js';
import ReactMapboxGl from 'react-mapbox-gl';
import MapboxDraw from '#mapbox/mapbox-gl-draw/dist/mapbox-gl-draw'
console.log(MapboxDraw)
I just get an empty object.
I am using create-react-app. Do I need to use a different webpack .config file.
What is the best way to import mapbox modules like this?
If you're using webpack, you need to alias to use mapbox-gl dist file, here is how I have done it using webpack2:
resolve: {
modules: ['src', 'node_modules'],
extensions: ['.js', '.jsx', '.json', '.css', '.svg'],
alias: {
// mapbox-gl related packages in webpack should use dist instead of the default src
'#mapbox/mapbox-gl-draw': path.resolve(root, 'node_modules/#mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.js'),
},
},
Variable "root" was defined earlier in the file to refer to the root directory of the project, on the same level as package.json etc.
The same problem can happen in react-map-gl-alt where you need to alias 'mapbox' to 'node_modules/mapbox/dist/distfilehere.js'.
To expand upon medv's answer, at current, in your package.json, you need to add the mapbox-gl v0.270 - v0.38.0, like:
"mapbox-gl": ">=0.27.0 <=0.38.0 when using "#mapbox/mapbox-gl-draw": "^1.0.1".
This is explained at the 'Requires' line here & if you look at the peerDependencies in the package.json.
I am trying to import famous into my application
When i create a breakpoint in the base index.html file in ember cli and look at what require seems to know about i see famo.us is there
in my brocfile i have tried the following
app.import('vendor/famous/famous.js', {
'famous/core/Context':''
});
app.import('vendor/famous/famous.js', {
'famous/core/Context':'default'
});
app.import('vendor/famous/famous.js');
this may be fixed by master of loader.js https://github.com/stefanpenner/loader.js/issues/25