can deactivate guard in standalone angular components is not working on browser back button

I am trying to implement deactivate guard in standalone angular components which to opens material dialog for confirm/cancel and to work in both – ts route change and browser back button.

I am trying to implement the can deactivate guard in angular standalone components. I have exactly the same implementation with a project which is with standard components and it works. This is the code of my can deactivate guard:

import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, CanDeactivateFn, RouterStateSnapshot } from '@angular/router';
import { MatDialog} from '@angular/material/dialog';
import { MxDialogComponent } from '../../mx/dialog';
import { TranslocoService } from '@ngneat/transloco';

import { CanComponentDeactivate } from '../../interfaces/can-component-deactivate.interface';

export const canComponentDeactivateGuard: CanDeactivateFn<CanComponentDeactivate> = (
  component: CanComponentDeactivate,
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
) => {
  const translocoService: TranslocoService = inject(TranslocoService);
  const dialog: MatDialog = inject(MatDialog);

  if (component.canDeactivate && component.canDeactivate()) {
    return true;
  }

  let message = translocoService.translate('discardDialog.message');
  let cancelAction = translocoService.translate('common.dialogs.discardDialog.cancelAction');

  const dialogRef = dialog.open(MxDialogComponent , {
    autoFocus: false,
      disableClose: true,
      restoreFocus: false,
      width: '620px'
  });
  dialogRef.componentInstance.title = translocoService.translate('common.dialogs.discardDialog.title');
  dialogRef.componentInstance.messageBottom = message;
  dialogRef.componentInstance.confirmActions = [
    { text: translocoService.translate('common.dialogs.discardDialog.confirmAction'), value: true, dataAttr: 'cx-dialog-confirm' }
  ];
  dialogRef.componentInstance.cancelActions = [
    { text: cancelAction, value: false, dataAttr: 'cx-dialog-cancel' }
  ];

  return dialogRef.afterClosed();
  // return window.confirm('Do you really want to cancel?');
};

In my standalone component i have implemented the

...
export class MyStandaloneComponent implements OnInit, OnDestroy, CanComponentDeactivate
....
  canDeactivate(): boolean {
    return !this.myHelperService.hasChanges;
  }

here is the interface for the CanComponentDeactivate

import { Observable } from 'rxjs';

export interface CanComponentDeactivate {
  canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

i have also set in the routes file of the standalone component:

canDeactivate: [canComponentDeactivateGuard],
    runGuardsAndResolvers: 'paramsOrQueryParamsChange',

Now if i try to naviage manually via typescript like:

this.router.navigateByUrl('/home');

The dialog is opened and working with both cancel and confirm. When i press the browser back button the code again runs including the code in the whole guard can deactivate but i end up going to the home page and the dialog is not even being shown. This scenario with this code is working fine when the component is not standalone. I have tried opening standard window confirm

return window.confirm('Do you really want to cancel?');

in the end of the guard instead of the mat dialog and it is working but when i try it with material dialog it is not.

New contributor

subradeep chandarma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật