Skip to content

task#08 bootstrap font awesome

bacn edited this page Sep 16, 2020 · 1 revision

Integration Bootstrap 4.0 with Sass and Font Awesome

Bootstrap

  1. Define sccs as the Default style to your Project
  2. Install Bootstrap Version 4.0
  3. Configure Bootstrap
  4. Refactor all .css files in .scss files
  5. Put bootstrap test code in app.component.html

Font Awesome

  1. Install Font Awesome
  2. Configure angular.json

Result

Compile and start the application with

$ ng serve


task8-result.png


src Folder structure

Source files containing elements, which are use by several other components or classes shall be moved to folders with name shared.

src

\- app
   \- auction-list
      \- auction-list.component.css
      \- auction-list.component.html
      \- auction-list.component.ts
      \- auction-list. component spec.ts
    \- mouse-event-display
       \- mouse-event-display.component.css
       \- mouse-event-display.component.html
       \- mouse-event-display.component.ts
       \- mouse-event-display. component spec.ts
   \- shared
      \- auction.ts
      \- auction-data.service.spec.ts
      \- auction-data.service.ts
      \- auction-data.ts
   \- app.module.ts
   \- app.component.html
   \- app.component.spec.ts
   \- app.component.ts
   \- app.component.scss
\- environments
   \- environment.prod.ts
   \- environment.ts
\- _variables.scss
\- app.constansts.ts
\- favicon.ico
\- index.html
\- main.ts
\- polyfills.ts
\- styles.scss
\- test.ts
\- tsconfig.app.jsop
\- tsconfig.spec.json
\- typings.d.ts

Hints Bootstrap integration

Set sccs as default style

see also: css preprocessor integration

Set the default style on an existing project:

$ ng config schematics.@schematics/angular:component.styleext scss

Install Bootstrap Version 4.0

Be aware...the description under the following link is incomplete.

see also: Bootstrap 4.0 integration

Bootstrap is a popular CSS framework which can be used within an Angular project. Install bootstrap to your project as a dependency. Using the --save option the dependency will be saved in package.json.

$ npm install bootstrap --save

Configure Bootstrap with scss

Now that the project is set up it must be configured to include the bootstrap CSS.

If the project wasn't using SCSS from the beginning ...rename styles.css in styles.sccs in src/. In styles.scss add the following:

@import 'variables';
@import '~bootstrap/scss/bootstrap';

Add the popper.js and jquery.js to the file package.json.

  "dependencies": {
 ...
    "jquery": "^3.5.1",
    "popper.js": "^1.16.1",
 ...
  },

Run npm install.

  • Open the file angular.json from the root of your project.
  • Under the property apps the first item in that array is the default application.
  • There is a property styles which allows external global styles to be applied to your application.
  • It should look like the following when you are done:
 "build": {
   "options": {
    ...
        "styles": [
            "./node_modules/bootstrap/dist/css/bootstrap.min.css",
            "src/styles.scss"
        ],
        "scripts": [
           "./node_modules/jquery/dist/jquery.min.js",
           "./node_modules/bootstrap/dist/js/bootstrap.min.js",
           "./node_modules/popper.js/dist/umd/popper.min.js"
        ],
   }
 }

Put the same entry in angular.json under test.

Create empty file _variables.scss in src/. Add the following Code to _variables.scss

$icon-font-path: '../node_modules/bootstrap-sass/assets/fonts/bootstrap/'; 

Refactor all existing .css files in .scss files. The style definition of the decorator class (styleUrls: ['./app.component.scss']) should be adjusted by the refactoring process.

If you have started the projekt with css and not with scss refactor the three files of the existing components.

  styleUrls: ['./auction-list.component.scss']
  styleUrls: ['./mouse-event-display.component.scss']
  styleUrls: ['./app.component.scss']

Testing

Open app.component.html and add the following markup:

<button class="btn btn-primary">Test Button</button>

To ensure your variables are used open _variables.scss and add the following:

$primary: red;

Hints Font Awesome Integration

see also: font awesome integration

Install the font-awesome library and add the dependency to package.json...

npm install --save font-awesome

To add Font Awesome CSS icons to your app...

  • Open the file angular.json from the root of your project.
  • Under the property apps the first item in that array is the default application.
  • There is a property styles which allows external global styles to be applied to your application.
  • It should look like the following when you are done:
 "styles": [
   "./node_modules/bootstrap/dist/css/bootstrap.min.css",
   "./node_modules/font-awesome/css/font-awesome.min.css",
   "src/styles.scss"
],

Enter the line two times in angular.json (one times under build and one times under test)

Add the import to the file styles.scss:

@import '~font-awesome/css/font-awesome.css';

Add the following to _variables.scss:

$fa-font-path : '../node_modules/font-awesome/fonts';

Run ng serve to run your application in develop mode, and navigate to http://localhost:4200. To verify Font Awesome has been set up correctly, change src/app/app.component.html to the following...

<h1>
  {{title}} <i class="fa fa-check"></i>
</h1>

After saving this file, return to the browser to see the Font Awesome icon next to the app title.

Clone this wiki locally