A Look Into Angular Material Design And Fixing Issues During Installation🔧If Any

Introduction

 
In this article, we will learn how to build Material design with a fonts using Angular and Bootstrap 4. Also, we will learn steps to fix issues in various ways during the installation of the Angular Material package and its use.
 

Prerequisites

  • Node
  • Npm
  • Angular CLI
  • TypeScript
  • Visual Studio Code
Note
Before going through this session, please visit my previous articles related to Angular 7 applications.
Step 1
 
In this step, you need to install Angular Material, Angular CDK, and Angular Animations. For this, you can use either the npm or yarn command-line tool to install packages. Run the command as mentioned below.
 
PS C:\Angular7\Dev\angular7app> npm install --save @angular/material @angular/cdk @angular/animations
 
Step 2
 
After the animations package is installed, import BrowserAnimationsModule into your project to activate the animations feature support. For this, put the below code in the app.module.ts file to import the animation support in Angular application.
  1. import {BrowserAnimationsModule} from '@angular/platform-browser/animations';  
Then, inside the imports section, add the BrowserAnimationsModule to activate the animation support.
  1. imports: [    
  2.    BrowserModule,    
  3.    AppRoutingModule,    
  4.    BrowserAnimationsModule,    
  5.    AngularFireModule.initializeApp(environment.firebase)    
  6. ],     
Step 3
 
Import the NgModule for each and every component as per your requirement. For the button component, I have added the below code to import MatButtonModule. Repeat the same steps as per step 2.
  1. import {MatButtonModule} from '@angular/material/button';    
Like this, for checkbox component, I have added the below code to import MatCheckboxModule. Repeat the same steps as per step 2.
  1. import {MatCheckboxModule} from '@angular/material/checkbox';  
Step 4
 
In this step,  a theme is required to apply all of the theme styles to our Angular application. For this, add the below line of code in styles.scss file.
  1. @import "~@angular/material/prebuilt-themes/indigo-pink.css";  
Step 5
 
Then, we need to add HammerJS to your application via npm. Some components depend on HammerJS for gestures. In order to get the full feature-set of these components, HammerJS should be required.
 
Use the following command to install HammerJS via NPM.
 
PS C:\Angular7\Dev\angular7app> npm install --save hammerjs
 
Step 6
 
After successful installation of HammerJS, import it on app.module.ts file of Angular application. Then put the below code in app.module.ts file of Angular application.
  1. import 'hammerjs';  
Step 7
 
Here, I need to add mat-icon component with the Material Design Icons. Then, load the icon font in your index.html. It supports any font. To do so, add the below line of code in index.html file.
  1. <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">    
  2. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">    
Step 8
 
In this step, I added a checkbox control which is enhanced with Material Design styling and animations. To do so, I opened the app.component.html file and added the below code.
  1. <mat-checkbox>Check me!</mat-checkbox>  

OUTPUT

 

Step 9
 
In this step, I added a button control which is enhanced with Material Design styling and ink ripples. To do so, I opened the app.component.html file and added the below code. 
  1. <div class="example-button-row">    
  2.   <button mat-raised-button>Basic</button>    
  3.   <button mat-raised-button color="primary">Primary</button>    
  4.   <button mat-raised-button color="accent">Accent</button>    
  5.   <button mat-raised-button color="warn">Warn</button>    
  6.   <button mat-raised-button disabled>Disabled</button>    
  7.   <a mat-raised-button routerLink=".">Link</a>    
  8. </div>     
mat-raised-button attribute descibes rectangular contained button w/ elevation
 
OUTPUT
 
Step 10
 
In this step, I added icon fonts and SVG icons to use vector-based icons in your app. For example: Like button in Facebook site. To do so, I added the below code in app.module.ts to import the service to register and display icons used by the <mat-icon> component. 
  1. import {MatIconModule} from '@angular/material/icon';  
  1. imports: [  
  2.     BrowserModule,  
  3.     BrowserAnimationsModule,  
  4.     MatButtonModule,  
  5.     MatCheckboxModule,  
  6.     AppRoutingModule,  
  7.     MatIconModule  
  8.   ],  
To show an output, add the below code in app.component.html file.
  1. <mat-icon aria-hidden="false" aria-label="Example home icon">home</mat-icon>  

OUTPUT

Step 11
 
In this, I have added API reference for Angular Material toolbar. To do so, I need to add the below code in app.module.ts file.
  1. import {MatToolbarModule} from '@angular/material/toolbar';  
  1. imports: [  
  2.     BrowserModule,  
  3.     BrowserAnimationsModule,  
  4.     MatButtonModule,  
  5.     MatCheckboxModule,  
  6.     AppRoutingModule,  
  7.     MatIconModule,  
  8.     MatToolbarModule  
  9.   ],  
Here, I will show you Multi-row toolbar with Material Design. To do so, I added the below code in app.component.html file.
  1. <mat-toolbar color="primary">    
  2.     <mat-toolbar-row>    
  3.       <span>My Name Is Satyaprakash</span>    
  4.     </mat-toolbar-row>    
  5.       
  6.     <mat-toolbar-row>    
  7.       <span>Right Mark</span>    
  8.       <span class="example-spacer"></span>    
  9.       <mat-icon class="example-icon" aria-hidden="false" aria-label="Example user verified icon">verified_user</mat-icon>    
  10.     </mat-toolbar-row>    
  11.       
  12.     <mat-toolbar-row>    
  13.       <span>Love</span>    
  14.       <span class="example-spacer"></span>    
  15.       <mat-icon class="example-icon" aria-hidden="false" aria-label="Example heart icon">favorite</mat-icon>    
  16.       <mat-icon class="example-icon" aria-hidden="false" aria-label="Example delete icon">delete</mat-icon>    
  17.     </mat-toolbar-row>    
  18.   </mat-toolbar>    

OUTPUT

Steps to solve issues during installation of Material Design package

 
During the installation of the Material design package, if you find a couple of vulnerabilities, then run 'npm audit fix' command to fix them. Check the below image,
 
 
 
If you get the error like "TypeError: Object(…) is not a function for angular-material" while using angular material, then you need to apply the below steps.
 
Reason
 
You can't use @angular/material and @angular/cdk version 8.1.1 since your angular dependencies is still at 7.3.7. So, download the version 7.3.7; To do so,
 
Remove this line as well: "angular-material": "^1.1.19", It is for Angular 1, not Angular 2+ from package.json file. Then, install dependencies with npm install and run the command: ng add @angular/material@^7.3.7
 
After successful installation, check your package.json file as shown below.
 
 
Another way to set-up the environment by avoiding the issue as mentioned below, 
 
Reason
 
The use of @angular/cdk and @angular/material in version 8 together with @angular/core @angular/common 7. If you want to stay at Angular 7 then uninstall cdk and material and install the specific CDK and material for version 7. Run below command,
 
npm uninstall @angular/cdk @angular/material

To find out the version 7 specific run below command,

npm show @angular/cdk versions
npm show @angular/material versions


and then install the specific one by running below command,

npm install @angular/[email protected]

Or just upgrade your angular by running the below command,

npm install -g @angular/cli@latest
ng update @angular/cli @angular-devkit/build-angular
ng update @angular/core @angular/cdk @angular/material
 
Then it asks to choose a prebuilt theme name as shown below,
 
 
Say 'y' when it asks you about adding hammer.js and animations module.
 
 

Steps to terminate batch job and reset terminal to run command

 
First type Ctrl+C => (^C) then its asks to "Terminate Batch Job"(Terminate batch job (Y/N)? y). You should say "Y". Then you are able to type command on same terminal window instead of open new terminal. Check the below image,
 
 

Summary

 
In this article, we have learned,
  • Intro to Angular Material design
  • Set-up Angular Material design
  • Import the NgModule for each and every component as per your requirement
  • Fix issues at the time of use of Angular Material design


Similar Articles