top of page

How To Use Sweetalert In Angular 10

Updated: Jun 12, 2022


In this article, we will learn how to use sweetalert2 in Angular 10 applications. Sweetalert2 is used to display alert messages.


  • Basic Knowledge of Angular 2 or higher

  • Visual Studio Code

  • Node and NPM installed

  • Bootstrap

Create an Angular project by using the following command.

ng new AngApp  

Open this project in Visual Studio Code and install Bootstrap by using the following command. 

npm install bootstrap --save        

Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.

@import '~bootstrap/dist/css/bootstrap.min.css';    

Install sweetalert2 

Now install sweetalert2  by using the following command.

npm install sweetalert2  

Now open index.html file and add the following line.

<script src=""></script>  

Now create a new component by using the following command.

ng new Alertmsg  

Now open Alertmsg.component.html file and add the following code, 

<div class="row">  
    <div class="col-sm-12 btn btn-primary">  
      Angular Sweetalert Demo  
  <div style="padding: 5px; margin: 5px;">  
    <button class="btn btn-info" style="margin-right: 10px;margin-left: 10px;"(click)="simpleAlert()">Simple Alert</button>  
    <button class="btn btn-success" style="margin-right: 10px;margin-left: 10px;" (click)="alertWithSuccess()">Alert with Success</button>  
    <button class="btn btn-primary" style="margin-right: 10px;margin-left: 10px;" (click)="confirmBox()">Confirm Box</button>  
    <button class="btn btn-danger" style="margin-right: 10px;margin-left: 10px;" (click)="erroalert()">Error Alert</button>  
    <button class="btn btn-warning" style="margin-right: 10px;margin-left: 10px;" (click)="topend()">Top End</button>  

Now open Alertmsg.component.tsfile and add the following code,

import { Component, OnInit } from '@angular/core';  
import Swal from 'sweetalert2/dist/sweetalert2.js';  
  selector: 'app-alertmsg',  
  templateUrl: './alertmsg.component.html',  
  styleUrls: ['./alertmsg.component.css']  
export class AlertmsgComponent implements OnInit {  
  simpleAlert(){'Hello Angular');  
  alertWithSuccess(){'Thank you...', 'You submitted successfully!', 'success')  
      icon: 'error',  
      title: 'Oops...',  
      text: 'Something went wrong!',  
      footer: '<a href>Why do I have this issue?</a>' 
      position: 'top-end',  
      icon: 'success',  
      title: 'Your work has been saved',  
      showConfirmButton: false,  
      timer: 1500  
      title: 'Are you sure want to remove?',  
      text: 'You will not be able to recover this file!',  
      icon: 'warning',  
      showCancelButton: true,  
      confirmButtonText: 'Yes, delete it!',  
      cancelButtonText: 'No, keep it' 
    }).then((result) => {  
 if (result.value) {   
 'Your imaginary file has been deleted.',  
      } else if (result.dismiss === Swal.DismissReason.cancel) {   
 'Your imaginary file is safe :)',  

Open app.moudle.ts file and add the  following code:

import { BrowserModule } from '@angular/platform-browser';  
import { NgModule } from '@angular/core';  
import { AppRoutingModule } from './app-routing.module';  
import { AppComponent } from './app.component';  
import { AlertmsgComponent } from './alertmsg/alertmsg.component';  
  declarations: [  
  imports: [  
  providers: [],  
  bootstrap: [AppComponent]  
export class AppModule { }  

Now open app.component.html file and add the following code,


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


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

Source: C#Corner - Sanwar Ranwa



bottom of page