How To Use Sweetalert In Angular 10

Introduction

 
In this article, we will learn how to use sweetalert2 in Angular 10 applications. Sweetalert2 is used to display alert messages.
 
Prerequisites
  • Basic Knowledge of Angular 2 or higher
  • Visual Studio Code
  • Node and NPM installed
  • Bootstrap
Create an Angular project by using the following command.
  1. ng new AngApp  
Open this project in Visual Studio Code and install Bootstrap by using the following command. 
  1. npm install bootstrap --save        
Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
  1. @import '~bootstrap/dist/css/bootstrap.min.css';    

Install sweetalert2 

 
Now install sweetalert2  by using the following command.
  1. npm install sweetalert2  
Now open index.html file and add the folloiwng line.
  1. <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>  
Now create a new component by using the following command.
  1. ng new Alertmsg  
Now open Alertmsg.component.html file and add the following code, 
  1. <div class="row">  
  2.     <div class="col-sm-12 btn btn-primary">  
  3.       Angular Sweetalert Demo  
  4.     </div>  
  5.   </div>  
  6.   <div style="padding: 5px;margin: 5px;">  
  7.     <button class="btn btn-info" style="margin-right: 10px;margin-left: 10px;"(click)="simpleAlert()">Simple Alert</button>  
  8.     <button class="btn btn-success" style="margin-right: 10px;margin-left: 10px;" (click)="alertWithSuccess()">Alert with Success</button>  
  9.     <button class="btn btn-primary" style="margin-right: 10px;margin-left: 10px;" (click)="confirmBox()">Confirm Box</button>  
  10.     <button class="btn btn-danger" style="margin-right: 10px;margin-left: 10px;" (click)="erroalert()">Error Alert</button>  
  11.     <button class="btn btn-warning" style="margin-right: 10px;margin-left: 10px;" (click)="topend()">Top End</button>  
  12.   </div>  
 Now open Alertmsg.component.tsfile and add the following code,
  1. import { Component, OnInit } from '@angular/core';  
  2. import Swal from 'sweetalert2/dist/sweetalert2.js';  
  3. @Component({  
  4.   selector: 'app-alertmsg',  
  5.   templateUrl: './alertmsg.component.html',  
  6.   styleUrls: ['./alertmsg.component.css']  
  7. })  
  8. export class AlertmsgComponent implements OnInit {  
  9.   
  10.   ngOnInit(){  
  11.     
  12.   }  
  13.     
  14.   simpleAlert(){  
  15.     Swal.fire('Hello Angular');  
  16.   }  
  17.     
  18.   alertWithSuccess(){  
  19.     Swal.fire('Thank you...''You submitted succesfully!''success')  
  20.   }  
  21.   erroalert()  
  22.   {  
  23.     Swal.fire({  
  24.       icon: 'error',  
  25.       title: 'Oops...',  
  26.       text: 'Something went wrong!',  
  27.       footer: '<a href>Why do I have this issue?</a>'  
  28.     })  
  29.   }  
  30.   topend()  
  31.   {  
  32.     Swal.fire({  
  33.       position: 'top-end',  
  34.       icon: 'success',  
  35.       title: 'Your work has been saved',  
  36.       showConfirmButton: false,  
  37.       timer: 1500  
  38.     })  
  39.   }  
  40.   confirmBox(){  
  41.     Swal.fire({  
  42.       title: 'Are you sure want to remove?',  
  43.       text: 'You will not be able to recover this file!',  
  44.       icon: 'warning',  
  45.       showCancelButton: true,  
  46.       confirmButtonText: 'Yes, delete it!',  
  47.       cancelButtonText: 'No, keep it'  
  48.     }).then((result) => {  
  49.       if (result.value) {  
  50.         Swal.fire(  
  51.           'Deleted!',  
  52.           'Your imaginary file has been deleted.',  
  53.           'success'  
  54.         )  
  55.       } else if (result.dismiss === Swal.DismissReason.cancel) {  
  56.         Swal.fire(  
  57.           'Cancelled',  
  58.           'Your imaginary file is safe :)',  
  59.           'error'  
  60.         )  
  61.       }  
  62.     })  
  63.   }  
  64. }  
Open app.moudle.ts file and add the  following code:
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { AppRoutingModule } from './app-routing.module';  
  4. import { AppComponent } from './app.component';  
  5. import { AlertmsgComponent } from './alertmsg/alertmsg.component';  
  6.   
  7. @NgModule({  
  8.   declarations: [  
  9.     AppComponent,  
  10.     AlertmsgComponent  
  11.   ],  
  12.   imports: [  
  13.     BrowserModule,  
  14.     AppRoutingModule  
  15.   ],  
  16.   providers: [],  
  17.   bootstrap: [AppComponent]  
  18. })  
  19. export class AppModule { }  
Now open app.component.html file and add the following code,
  1. <app-alertmsg></app-alertmsg>  
Now run the project by using the following command: 'npm start'
 
Now click on the given button and check alert message.
 
Simple Alert message
 
Confirm message
 
 

Summary


In this article, we learned how to add sweetalert2 in Angular 10 applications.