Angular 12 generate QR code example; In this tutorial, i am going to show you how to generate qr code in angular 12 app using npm angularx-qrcode –save.
Well, the QR – which stands for “quick response” – code is basically a barcode on steroids. While the barcode holds information horizontally, the QR code does so both horizontally and vertically. This enables the QR code to hold over a hundred times more information.
How to Create/Generate QR Code in Angular 12 App
- Step 1 – Create New Angular App
- Step 2 – Install angularx-qrcode npm Package
- Step 3 – Import QR Code Library on app.module.ts File
- Step 4 – Add Code on View File
- Step 5 – Add Code On Component ts File
- Step 6 – Start Angular App
Step 1 – Create New Angular App
First of all, open your terminal and execute the following command on it to install angular app:
ng new my-new-app
Then execute the following command on terminal to install angular material:
ng add @angular/material
Step 2 – Install angularx-qrcode npm Package
In this step, you need to install angularx-qrcode in our angular application. So, open your terminal and execute the following command:
npm install angularx-qrcode --save
Step 3 – Import QR Code Library on app.module.ts File
Go to src/app directory and open app.module.ts file. Then add the following code into it:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { QRCodeModule } from 'angularx-qrcode'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, QRCodeModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Step 4 – Add Code on View File
Create html and for display qr code in angular app. So, visit src/app/app.component.html and update the following code into it:
<h1>How to Generate QR Code in Angular 12? - lara.com</h1> <qrcode [qrdata]="'myAngularxQrCode'" [width]="256" [errorCorrectionLevel]="'M'"></qrcode>
Step 5 – Add Code On Component ts File
Go to the src/app directory and open app.component.ts. Then add the following code into component.ts file:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { public myAngularxQrCode: string = null; constructor () { this.myAngularxQrCode = 'ItSoluionStuff.com'; } }
Step 6 – Start Angular App
Execute the following commands on terminal to start angular app:
ng serve