Angular Material: How to close all mat-dialogs and sweet-alerts on logout Angular Material: How to close all mat-dialogs and sweet-alerts on logout angular angular

Angular Material: How to close all mat-dialogs and sweet-alerts on logout


This is what i have done to close any open mat-dialog throughout the application:

import {MatDialog} from '@angular/material';export class myClass {constructor(private dialogRef: MatDialog) {}logOut(){  this.dialogRef.closeAll();}}

If you would like to close only a specific dialog you can loop through dialogRef.openDialogs and close the respective dialog using close()

This is how you can close any open/active sweet alert dialog:

const sweetAlertCancel = document.querySelector('.swal2-cancel') as HTMLElement;if (sweetAlertCancel) {    sweetAlertCancel.click(); //only if cancel button exists}const sweetAlertConfirm = document.querySelector('.swal2-confirm') as HTMLElement;if (sweetAlertConfirm) {   sweetAlertConfirm.click(); //if cancel doesn't exist , confirm is the equivalent for Ok button}

Unlike material-dialog there is no method available to close or hide all open sweet alert dialog's. This is what i'm able to do so far.


For anyone looking for an answer, if method .closeAll() is not available on DialogRef (e.g. if using newer @angular/material components):

import {MatDialog} from '@angular/material/dialog';constructor(matDialog: MatDialog) {…}logout() {    this.matDialog.closeAll();}